C1 cms 复合C1-使用MVC渲染片段等
在复合C1中,我使用Razor语法创建主布局。为了加快加载速度,建议将脚本放在EndBody标记之前,而不是放在head标记内部。这就是为什么我将jQuery和其他脚本放在EndBody标记之前 当我在引用jQuery的JavaScript中使用Razor函数时,会出现一个错误,因为jQuery还没有加载。Razor函数中的HTML在加载jQuery脚本之前输出: 未捕获引用错误:$未定义 在MVC中,我可以在主布局中使用RenderSection来实现这一点(在主布局脚本下面呈现JavaScript)C1 cms 复合C1-使用MVC渲染片段等,c1-cms,C1 Cms,在复合C1中,我使用Razor语法创建主布局。为了加快加载速度,建议将脚本放在EndBody标记之前,而不是放在head标记内部。这就是为什么我将jQuery和其他脚本放在EndBody标记之前 当我在引用jQuery的JavaScript中使用Razor函数时,会出现一个错误,因为jQuery还没有加载。Razor函数中的HTML在加载jQuery脚本之前输出: 未捕获引用错误:$未定义 在MVC中,我可以在主布局中使用RenderSection来实现这一点(在主布局脚本下面呈现JavaScr
@RenderSection("FooterScripts", false)
然后在我的视图中,我可以定义这样一个部分:
@section FooterScripts {
<script type="text/javaScript">
$(function () {
...
});
</script>
}
@节页脚脚本{
$(函数(){
...
});
}
这将在最终HTML中的正确位置呈现HTML。在复合C1中可以这样做吗?即使Intellisence告诉我RenderSection可用,我也无法让它工作。没有内置方法将HTML标记从C1函数插入布局中的特定位置 实现您自己的逻辑的可能方法是:
@inherits RazorFunction
@using Composite.Examples
@functions {
}
@{
LayoutHelper.AddDelayedScript(Script());
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
Inserting a script at the bottom of a page
</body>
</html>
@helper Script() {
<script type="text/javascript">
alert("I'm inserted!");
</script>
}
@继承RazorFunction
@使用Composite.Examples
@功能{
}
@{
LayoutHelper.AddDelayedScript(Script());
}
在页面底部插入脚本
@助手脚本(){
警报(“我已插入!”);
}
布局代码:
....
@LayoutHelper.RenderDelayedScripts()
</body>
</html>
。。。。
@LayouthHelper.RenderLayedScript()文件
类LayouthHelper,在应用程序代码中定义:
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace Composite.Examples
{
public static class LayoutHelper
{
private const string HttpItems_Key = "delayedscripts";
public static void AddDelayedScript(IHtmlString script)
{
var context = HttpContext.Current;
lock (context.Items.SyncRoot)
{
if (!context.Items.Contains(HttpItems_Key))
{
context.Items.Add(HttpItems_Key, new List<IHtmlString>());
}
(context.Items[HttpItems_Key] as List<IHtmlString>).Add(script);
}
}
public static IHtmlString RenderDelayedScripts()
{
var context = HttpContext.Current;
var sb = new StringBuilder();
if (context.Items.Contains(HttpItems_Key))
{
foreach (var delayedscript in context.Items[HttpItems_Key] as IEnumerable<IHtmlString>)
{
sb.Append(delayedscript.ToHtmlString());
}
}
return new HtmlString(sb.ToString());
}
}
}
使用System.Collections.Generic;
使用系统文本;
使用System.Web;
名称空间复合。示例
{
公共静态类布局帮助器
{
私有常量字符串HttpItems_Key=“delayedscripts”;
公共静态void AddDelayedScript(IHtmlString脚本)
{
var context=HttpContext.Current;
锁定(context.Items.SyncRoot)
{
如果(!context.Items.Contains(HttpItems_Key))
{
Add(HttpItems_键,newlist());
}
(context.Items[HttpItems\u Key]作为列表)。添加(脚本);
}
}
公共静态IHtmlString renderLayedScript()
{
var context=HttpContext.Current;
var sb=新的StringBuilder();
if(context.Items.Contains(HttpItems_Key))
{
foreach(上下文中的var delayedscript.Items[HttpItems\u Key]作为IEnumerable)
{
sb.Append(delayedscript.ToHtmlString());
}
}
返回新的HtmlString(sb.ToString());
}
}
}
没有内置方法将html标记从C1函数插入布局中的特定位置
实现您自己的逻辑的可能方法是:
@inherits RazorFunction
@using Composite.Examples
@functions {
}
@{
LayoutHelper.AddDelayedScript(Script());
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
Inserting a script at the bottom of a page
</body>
</html>
@helper Script() {
<script type="text/javascript">
alert("I'm inserted!");
</script>
}
@继承RazorFunction
@使用Composite.Examples
@功能{
}
@{
LayoutHelper.AddDelayedScript(Script());
}
在页面底部插入脚本
@助手脚本(){
警报(“我已插入!”);
}
布局代码:
....
@LayoutHelper.RenderDelayedScripts()
</body>
</html>
。。。。
@LayouthHelper.RenderLayedScript()文件
类LayouthHelper,在应用程序代码中定义:
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace Composite.Examples
{
public static class LayoutHelper
{
private const string HttpItems_Key = "delayedscripts";
public static void AddDelayedScript(IHtmlString script)
{
var context = HttpContext.Current;
lock (context.Items.SyncRoot)
{
if (!context.Items.Contains(HttpItems_Key))
{
context.Items.Add(HttpItems_Key, new List<IHtmlString>());
}
(context.Items[HttpItems_Key] as List<IHtmlString>).Add(script);
}
}
public static IHtmlString RenderDelayedScripts()
{
var context = HttpContext.Current;
var sb = new StringBuilder();
if (context.Items.Contains(HttpItems_Key))
{
foreach (var delayedscript in context.Items[HttpItems_Key] as IEnumerable<IHtmlString>)
{
sb.Append(delayedscript.ToHtmlString());
}
}
return new HtmlString(sb.ToString());
}
}
}
使用System.Collections.Generic;
使用系统文本;
使用System.Web;
名称空间复合。示例
{
公共静态类布局帮助器
{
私有常量字符串HttpItems_Key=“delayedscripts”;
公共静态void AddDelayedScript(IHtmlString脚本)
{
var context=HttpContext.Current;
锁定(context.Items.SyncRoot)
{
如果(!context.Items.Contains(HttpItems_Key))
{
Add(HttpItems_键,newlist());
}
(context.Items[HttpItems\u Key]作为列表)。添加(脚本);
}
}
公共静态IHtmlString renderLayedScript()
{
var context=HttpContext.Current;
var sb=新的StringBuilder();
if(context.Items.Contains(HttpItems_Key))
{
foreach(上下文中的var delayedscript.Items[HttpItems\u Key]作为IEnumerable)
{
sb.Append(delayedscript.ToHtmlString());
}
}
返回新的HtmlString(sb.ToString());
}
}
}
谢谢,这是一个非常顺利的解决方案谢谢,这是一个非常顺利的解决方案