When I loop though an IEnumerable in my DotLiquid template
{% for block in Blocks -%}
// this works
{{ block.Structure }}
// this doesn't
{% RenderObject block.Structure %}
{% endfor -%}
I can render a member directly via block.Structure but I don’t know how to access this Structure object in my own tag class RenderObject:
public class RenderObject : Tag
{
private string _tagName;
private string _markup;
public override void Initialize(string tagName, string markup, List<string> tokens)
{
_tagName = tagName;
_markup = markup.Trim();
base.Initialize(tagName, markup, tokens);
}
public override void Render(Context context, TextWriter result)
{
// HERE COMES THE QUESTION
// How to access the block.Structure object here?
var structure = ?
You can use the
contextobject that’s passed in to yourRenderObject.Rendermethod. There’s an indexer onContextthat resolves variable names into variables. (And theFortag places the loop variable, i.e.blockin your example, into thecontext.)The remaining problem is getting the variable name (“block.Structure”). Fortunately, that’s exactly what the
markupvariable, passed in toRenderObject.Initialize, is there for.So this should work: