Added meta tag registration and rendering for theming.

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4044210
This commit is contained in:
ErikPorter
2009-12-17 08:03:48 +00:00
parent 91c59ba232
commit d3b3829314
4 changed files with 30 additions and 7 deletions

View File

@@ -28,7 +28,7 @@ namespace Orchard.Mvc.Html {
pageTitleBuilder.AddTitleParts(titleParts); pageTitleBuilder.AddTitleParts(titleParts);
return MvcHtmlString.Create(pageTitleBuilder.GenerateTitle()); return MvcHtmlString.Create(html.Encode(pageTitleBuilder.GenerateTitle()));
} }
public static void Zone<TModel>(this HtmlHelper<TModel> html, string zoneName, string partitions) where TModel : BaseViewModel { public static void Zone<TModel>(this HtmlHelper<TModel> html, string zoneName, string partitions) where TModel : BaseViewModel {
@@ -51,6 +51,10 @@ namespace Orchard.Mvc.Html {
html.Zone(zoneName, () => html.RenderBody()); html.Zone(zoneName, () => html.RenderBody());
} }
public static void RegisterMeta(this HtmlHelper html, string name, string content) {
html.Resolve<IResourceManager>().RegisterMeta(name, content);
}
public static void RegisterStyle(this HtmlHelper html, string fileName) { public static void RegisterStyle(this HtmlHelper html, string fileName) {
html.Resolve<IResourceManager>().RegisterStyle(fileName); html.Resolve<IResourceManager>().RegisterStyle(fileName);
} }
@@ -58,5 +62,9 @@ namespace Orchard.Mvc.Html {
public static void RegisterScript(this HtmlHelper html, string fileName) { public static void RegisterScript(this HtmlHelper html, string fileName) {
html.Resolve<IResourceManager>().RegisterHeadScript(fileName); html.Resolve<IResourceManager>().RegisterHeadScript(fileName);
} }
public static void RegisterFootScript(this HtmlHelper html, string fileName) {
html.Resolve<IResourceManager>().RegisterFootScript(fileName);
}
} }
} }

View File

@@ -2,9 +2,11 @@ using System.Web.Mvc;
namespace Orchard.UI.Resources { namespace Orchard.UI.Resources {
public interface IResourceManager : IDependency { public interface IResourceManager : IDependency {
void RegisterMeta(string name, string content);
void RegisterStyle(string fileName); void RegisterStyle(string fileName);
void RegisterHeadScript(string fileName); void RegisterHeadScript(string fileName);
void RegisterFootScript(string fileName); void RegisterFootScript(string fileName);
MvcHtmlString GetMetas();
MvcHtmlString GetStyles(); MvcHtmlString GetStyles();
MvcHtmlString GetHeadScripts(); MvcHtmlString GetHeadScripts();
MvcHtmlString GetFootScripts(); MvcHtmlString GetFootScripts();

View File

@@ -14,6 +14,7 @@ namespace Orchard.UI.Resources {
BaseViewModel model = filterContext.Controller.ViewData.Model as BaseViewModel; BaseViewModel model = filterContext.Controller.ViewData.Model as BaseViewModel;
if (model != null) { if (model != null) {
model.Zones.AddAction("head:metas", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetMetas()));
model.Zones.AddAction("head:styles", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetStyles())); model.Zones.AddAction("head:styles", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetStyles()));
model.Zones.AddAction("head:scripts", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetHeadScripts())); model.Zones.AddAction("head:scripts", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetHeadScripts()));
model.Zones.AddAction("body:after", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetFootScripts())); model.Zones.AddAction("body:after", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetFootScripts()));

View File

@@ -9,8 +9,10 @@ namespace Orchard.UI.Resources {
public class ResourceManager : IResourceManager { public class ResourceManager : IResourceManager {
private readonly IThemeService _themeService; private readonly IThemeService _themeService;
private readonly IExtensionManager _extensionManager; private readonly IExtensionManager _extensionManager;
private const string MetaFormat = "\r\n<meta name=\"{0}\" content=\"{1}\" />";
private const string StyleFormat = "\r\n<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />"; private const string StyleFormat = "\r\n<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />";
private const string ScriptFormat = "\r\n<script type=\"text/javascript\" src=\"{0}\"></script>"; private const string ScriptFormat = "\r\n<script type=\"text/javascript\" src=\"{0}\"></script>";
private readonly Dictionary<string, string> _metas;
private readonly List<string> _styles; private readonly List<string> _styles;
private readonly List<string> _headScripts; private readonly List<string> _headScripts;
private readonly List<string> _footScripts; private readonly List<string> _footScripts;
@@ -19,29 +21,39 @@ namespace Orchard.UI.Resources {
IExtensionManager extensionManager) { IExtensionManager extensionManager) {
_themeService = themeService; _themeService = themeService;
_extensionManager = extensionManager; _extensionManager = extensionManager;
_styles = new List<string>(); //TODO: (erikpo) Not sure if generator should be initialized here or somewhere else
_headScripts = new List<string>(); _metas = new Dictionary<string, string>(20) {{"generator", "Orchard"}};
_footScripts = new List<string>(); _styles = new List<string>(10);
_headScripts = new List<string>(10);
_footScripts = new List<string>(5);
}
public void RegisterMeta(string name, string content) {
if (!string.IsNullOrEmpty(name) && !_metas.ContainsKey(name))
_metas.Add(name, content);
} }
public void RegisterStyle(string fileName) { public void RegisterStyle(string fileName) {
//TODO: (erikpo) Figure out best storage here
if (!string.IsNullOrEmpty(fileName) && !_styles.Contains(fileName)) if (!string.IsNullOrEmpty(fileName) && !_styles.Contains(fileName))
_styles.Add(fileName); _styles.Add(fileName);
} }
public void RegisterHeadScript(string fileName) { public void RegisterHeadScript(string fileName) {
//TODO: (erikpo) Figure out best storage here
if (!string.IsNullOrEmpty(fileName) && !_headScripts.Contains(fileName) && !_footScripts.Contains(fileName)) if (!string.IsNullOrEmpty(fileName) && !_headScripts.Contains(fileName) && !_footScripts.Contains(fileName))
_headScripts.Add(fileName); _headScripts.Add(fileName);
} }
public void RegisterFootScript(string fileName) { public void RegisterFootScript(string fileName) {
//TODO: (erikpo) Figure out best storage here
if (!string.IsNullOrEmpty(fileName) && !_headScripts.Contains(fileName) && !_footScripts.Contains(fileName)) if (!string.IsNullOrEmpty(fileName) && !_headScripts.Contains(fileName) && !_footScripts.Contains(fileName))
_footScripts.Add(fileName); _footScripts.Add(fileName);
} }
public MvcHtmlString GetMetas() {
return
MvcHtmlString.Create(string.Join("\r\n",
_metas.Select(m => string.Format(MetaFormat, m.Key, m.Value)).ToArray()));
}
public MvcHtmlString GetStyles() { public MvcHtmlString GetStyles() {
return GetFiles(_styles, StyleFormat, s => GetThemePath("/styles/" + s)); return GetFiles(_styles, StyleFormat, s => GetThemePath("/styles/" + s));
} }