Permalink
Browse files

Support for placeholders in React renderings.

  • Loading branch information...
JakobChristensen committed Feb 5, 2016
1 parent 53dafc1 commit 75c4b8305041bfe8c05a8ea4e4fdc2175b6743dd
@@ -10,17 +10,18 @@ namespace Sitecore.Pathfinder.HtmlFile.HtmlFiles
public class HtmlFileRenderingParser : RenderingParser
{
[NotNull]
- private static readonly Regex Regex = new Regex("\\{\\{\\%([^\\}]*)\\}\\}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
+ private static readonly Regex PlaceholderRegex = new Regex("\\{\\{\\%([^\\}]*)\\}\\}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
public HtmlFileRenderingParser() : base(".html", Constants.Templates.ViewRendering)
{
}
protected override IEnumerable<string> GetPlaceholders(string contents)
{
- var matches = Regex.Matches(contents);
+ var matches = PlaceholderRegex.Matches(contents);
- return matches.OfType<Match>().Select(i => i.Groups[1].ToString().Trim());
+ var placeholders = matches.OfType<Match>().Select(i => i.Groups[1].ToString().Trim());
+ return placeholders;
}
}
}
@@ -8,7 +8,7 @@
using Sitecore.IO;
using Sitecore.Pathfinder.Extensions;
-namespace Sitecore.Pathfinder.React
+namespace Sitecore.Pathfinder.React.Hooks
{
public class AddJsxScripts : IHook
{
@@ -1,43 +1,50 @@
// © 2015-2016 Sitecore Corporation A/S. All rights reserved.
+using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
+using System.Linq;
using React;
using React.Exceptions;
using React.TinyIoC;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
+using Sitecore.Mvc;
using Sitecore.Mvc.Presentation;
+using Sitecore.Pathfinder.Extensions;
using Sitecore.Web.UI.WebControls;
-namespace Sitecore.Pathfinder.Mvc.Presentation
+namespace Sitecore.Pathfinder.React.Mvc.Presentation
{
public class JsxRenderer : Renderer
{
- public static IReactEnvironment Environment
+ public JsxRenderer([Diagnostics.NotNull] Sitecore.Mvc.Presentation.Rendering rendering, [Diagnostics.NotNull] string filePath)
+ {
+ Rendering = rendering;
+ FilePath = filePath;
+ }
+
+ protected static IReactEnvironment Environment
{
get
{
- IReactEnvironment current;
try
{
- current = ReactEnvironment.Current;
+ return ReactEnvironment.Current;
}
- catch (TinyIoCResolutionException tinyIoCResolutionException)
+ catch (TinyIoCResolutionException ex)
{
- throw new ReactNotInitialisedException("ReactJS.NET has not been initialised correctly.", tinyIoCResolutionException);
+ throw new ReactNotInitialisedException("ReactJS.NET has not been initialised correctly.", ex);
}
-
- return current;
}
}
[Diagnostics.NotNull]
- public string FilePath { get; set; } = string.Empty;
+ protected string FilePath { get; }
[Diagnostics.NotNull]
- public Sitecore.Mvc.Presentation.Rendering Rendering { get; set; }
+ protected Sitecore.Mvc.Presentation.Rendering Rendering { get; }
public override void Render([Diagnostics.NotNull] TextWriter writer)
{
@@ -50,24 +57,51 @@ public override void Render([Diagnostics.NotNull] TextWriter writer)
writer.WriteLine($"<script>{reactComponent.RenderJavaScript()}</script>");
}
- private Item GetDataSourceItem()
- {
- return !string.IsNullOrEmpty(Rendering.DataSource) ? (Context.Database.GetItem(Rendering.DataSource) ?? Context.Item) : Context.Item;
- }
-
- private dynamic GetProps()
+ protected virtual dynamic GetProps()
{
dynamic props = new ExpandoObject();
- var dataSourceItem = GetDataSourceItem();
+ var propsDictionary = (IDictionary<string, object>)props;
+
+ dynamic placeholders = new ExpandoObject();
+ var placeholdersDictionary = (IDictionary<string, object>)placeholders;
+ propsDictionary["placeholders"] = placeholders;
+
+ var dataSourceItem = !string.IsNullOrEmpty(Rendering.DataSource) ? (Context.Database.GetItem(Rendering.DataSource) ?? Context.Item) : Context.Item;
foreach (Field field in dataSourceItem.Fields)
{
- if (field.Name.StartsWith("__"))
+ if (!field.Name.StartsWith("__"))
{
- continue;
+ propsDictionary.Add(field.Name, FieldRenderer.Render(dataSourceItem, field.Name));
}
+ }
- ((IDictionary<string, object>)props).Add(field.Name.ToLowerInvariant(), FieldRenderer.Render(dataSourceItem, field.Name));
+ var placeholdersField = Rendering.RenderingItem.InnerItem["Place Holders"];
+ if (string.IsNullOrEmpty(placeholdersField))
+ {
+ return props;
+ }
+
+ var controlId = Rendering.Parameters["id"] ?? string.Empty;
+ dynamic placeholderId = null;
+
+ var placeholderKeys = placeholdersField.Split(Constants.Comma, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToList();
+ foreach (var placeholderKey in placeholderKeys)
+ {
+ if (placeholderKey.StartsWith("$Id."))
+ {
+ if (placeholderId == null)
+ {
+ placeholderId = new ExpandoObject();
+ placeholdersDictionary["$Id"] = placeholderId;
+ }
+
+ ((IDictionary<string, object>)placeholderId)[placeholderKey.Mid(3)] = PageContext.Current.HtmlHelper.Sitecore().Placeholder(controlId + placeholderKey.Mid(3)).ToString();
+ }
+ else
+ {
+ placeholdersDictionary[placeholderKey] = PageContext.Current.HtmlHelper.Sitecore().Placeholder(placeholderKey).ToString();
+ }
}
return props;
@@ -5,9 +5,9 @@
using Sitecore.Mvc.Names;
using Sitecore.Mvc.Pipelines.Response.GetRenderer;
using Sitecore.Mvc.Presentation;
-using Sitecore.Pathfinder.Mvc.Presentation;
+using Sitecore.Pathfinder.React.Mvc.Presentation;
-namespace Sitecore.Pathfinder.Mvc.Response.GetRenderer
+namespace Sitecore.Pathfinder.React.Mvc.Response.GetRenderer
{
public class GetJsxRenderer : GetRendererProcessor
{
@@ -30,16 +30,12 @@ protected virtual Renderer GetRenderer([Diagnostics.NotNull] Rendering rendering
return null;
}
- if (!filePath.EndsWith(".jsx", StringComparison.OrdinalIgnoreCase))
+ if (filePath.EndsWith(".jsx", StringComparison.OrdinalIgnoreCase))
{
- return null;
+ return new JsxRenderer(rendering, filePath);
}
-
- return new JsxRenderer
- {
- FilePath = filePath,
- Rendering = rendering
- };
+
+ return null;
}
[Diagnostics.CanBeNull]
@@ -49,16 +45,16 @@ protected virtual string GetViewPath([Diagnostics.NotNull] Rendering rendering,
}
[Diagnostics.CanBeNull]
- private string GetViewPathFromLayoutItem([Diagnostics.NotNull] GetRendererArgs args)
+ private string GetViewPathFromInnerItem([Diagnostics.NotNull] Rendering rendering)
{
- var filePath = args.LayoutItem.ValueOrDefault(item => item.FilePath);
+ var filePath = rendering.RenderingItem.InnerItem["Path"];
return string.IsNullOrWhiteSpace(filePath) ? null : filePath;
}
[Diagnostics.CanBeNull]
- private string GetViewPathFromInnerItem([Diagnostics.NotNull] Rendering rendering)
+ private string GetViewPathFromLayoutItem([Diagnostics.NotNull] GetRendererArgs args)
{
- var filePath = rendering.RenderingItem.InnerItem["Path"];
+ var filePath = args.LayoutItem.ValueOrDefault(item => item.FilePath);
return string.IsNullOrWhiteSpace(filePath) ? null : filePath;
}
@@ -2,19 +2,25 @@
using System.Collections.Generic;
using System.Linq;
+using System.Text.RegularExpressions;
using Sitecore.Pathfinder.Languages.Renderings;
-namespace Sitecore.Pathfinder.React
+namespace Sitecore.Pathfinder.React.React
{
public class JsxRenderingParser : RenderingParser
{
+ private static readonly Regex PlaceholderRegex = new Regex("\\{[\\s]*this\\.props\\.placeholders\\.([^\\}]*)[\\s]*\\}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
+
public JsxRenderingParser() : base(".jsx", Constants.Templates.ViewRendering)
{
}
protected override IEnumerable<string> GetPlaceholders(string contents)
{
- return Enumerable.Empty<string>();
+ var matches = PlaceholderRegex.Matches(contents);
+
+ var placeholders = matches.OfType<Match>().Select(i => i.Groups[1].ToString().Trim());
+ return placeholders;
}
}
}
@@ -8,7 +8,7 @@
<ProjectGuid>{C735B4FA-4834-47D0-A0D7-51270AD3153E}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
- <RootNamespace>Sitecore.Pathfinder</RootNamespace>
+ <RootNamespace>Sitecore.Pathfinder.React</RootNamespace>
<AssemblyName>Sitecore.Pathfinder.React</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
@@ -134,7 +134,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="React\JsxRenderingParser.cs" />
- <Compile Include="React\AddJsxScripts.cs" />
+ <Compile Include="Hooks\AddJsxScripts.cs" />
<Compile Include="React\ReactExtension.cs" />
<Compile Include="Mvc\Presentation\JsxRenderer.cs" />
<Compile Include="Mvc\Response\GetRenderer\GetJsxRenderer.cs" />
@@ -11,7 +11,7 @@
</settings>
<hooks>
- <hook type="Sitecore.Pathfinder.React.AddJsxScripts, Sitecore.Pathfinder.React" />
+ <hook type="Sitecore.Pathfinder.React.Hooks.AddJsxScripts, Sitecore.Pathfinder.React" />
</hooks>
<pipelines>
@@ -22,7 +22,7 @@
<mvc.getRenderer>
<processor type="Sitecore.Pathfinder.Mvc.Response.GetRenderer.GetHtmlTemplateRenderer, Sitecore.Pathfinder.HtmlFile" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Response.GetRenderer.GetViewRenderer, Sitecore.Mvc']"/>
- <processor type="Sitecore.Pathfinder.Mvc.Response.GetRenderer.GetJsxRenderer, Sitecore.Pathfinder.React" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Response.GetRenderer.GetViewRenderer, Sitecore.Mvc']"/>
+ <processor type="Sitecore.Pathfinder.React.Mvc.Response.GetRenderer.GetJsxRenderer, Sitecore.Pathfinder.React" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Response.GetRenderer.GetViewRenderer, Sitecore.Mvc']"/>
</mvc.getRenderer>
</pipelines>
</sitecore>
@@ -24,10 +24,15 @@ public static T Resolve<T>([NotNull] this ICompositionService compositionService
{
Console.WriteLine(ex.Message);
- if (ex is System.Reflection.ReflectionTypeLoadException)
+ if (ex is ReflectionTypeLoadException)
{
var typeLoadException = ex as ReflectionTypeLoadException;
var loaderExceptions = typeLoadException.LoaderExceptions;
+
+ foreach (var loaderException in loaderExceptions)
+ {
+ Console.WriteLine(loaderException.Message);
+ }
}
throw;
@@ -39,15 +39,11 @@ public override void Parse(IParseContext context)
var rendering = context.Factory.Rendering(context.Project, context.Snapshot, context.DatabaseName, context.ItemPath, context.ItemName, context.FilePath, TemplateIdOrPath);
context.Project.AddOrMerge(rendering);
- // todo: make this configurable
- if (string.Equals(rendering.DatabaseName, "core", StringComparison.OrdinalIgnoreCase))
- {
- var contents = context.Snapshot.SourceFile.ReadAsText();
+ var contents = context.Snapshot.SourceFile.ReadAsText();
- var placeholders = GetPlaceholders(contents);
+ var placeholders = GetPlaceholders(contents);
- rendering.Placeholders.AddRange(placeholders);
- }
+ rendering.Placeholders.AddRange(placeholders);
context.Project.Ducats += 100;
}
@@ -3,19 +3,23 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
+using Sitecore.Pathfinder.Diagnostics;
using Sitecore.Pathfinder.Extensions;
namespace Sitecore.Pathfinder.Languages.Renderings
{
public class ViewRenderingParser : RenderingParser
{
+ [NotNull]
+ private static readonly Regex PlaceholderRegex = new Regex("\\@Html\\.Sitecore\\(\\)\\.Placeholder\\(([^\"\\)]*)\"([^\"]*)\"\\)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
+
public ViewRenderingParser() : base(".cshtml", Constants.Templates.ViewRendering)
{
}
protected override IEnumerable<string> GetPlaceholders(string contents)
{
- var matches = Regex.Matches(contents, "\\@Html\\.Sitecore\\(\\)\\.Placeholder\\(([^\"\\)]*)\"([^\"]*)\"\\)", RegexOptions.IgnoreCase);
+ var matches = PlaceholderRegex.Matches(contents);
var result = new List<string>();
foreach (var match in matches.OfType<Match>())
@@ -9,13 +9,16 @@ namespace Sitecore.Pathfinder.Languages.Renderings
{
public abstract class WebFormsRenderingParser : RenderingParser
{
+ [NotNull]
+ private static readonly Regex PlaceholderRegex = new Regex("<[^>]*Placeholder[^>]*Key=\"([^\"]*)\"[^>]*>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
+
protected WebFormsRenderingParser([NotNull] string fileExtension, [NotNull] string templateIdOrPath) : base(fileExtension, templateIdOrPath)
{
}
protected override IEnumerable<string> GetPlaceholders(string contents)
{
- var matches = Regex.Matches(contents, "<[^>]*Placeholder[^>]*Key=\"([^\"]*)\"[^>]*>", RegexOptions.IgnoreCase);
+ var matches = PlaceholderRegex.Matches(contents);
return matches.OfType<Match>().Select(i => i.Groups[1].ToString().Trim());
}

1 comment on commit 75c4b83

@alexshyba

This comment has been minimized.

Show comment
Hide comment
@alexshyba

alexshyba Feb 7, 2016

Yessssss! Thank you!!!

Yessssss! Thank you!!!

Please sign in to comment.