I have this code,but not working

132 views
Skip to first unread message

Matias Pereira

unread,
May 18, 2012, 8:12:49 PM5/18/12
to dotliquid
I have this code,but not working (0 is idCategory)
{% for category in 0|getcategorybyid%}
{{ category.name}}
{% endfor -%}

this code: {{ 0|getcategorybyid }} return > DotLiquidApi
+CategoriesDotLiquidApi+CategoriesDotLiquidApi+Categories
(i have 3 categories with id 0)

can you help?

Tim Jones

unread,
May 20, 2012, 10:22:31 AM5/20/12
to dotl...@googlegroups.com
Hi Matias,

Can you include your code for the getcategorybyid filter? I'm guessing that it returns a collection - which is why your second template outputs the type of the collection, since it doesn't know how to output the collection directly.

Your first template, with the {% for %} loop, wouldn't work, because the "for" tag doesn't allow an expression in place of the collection. That would be a nice feature, but it's not supported at the moment.

If you really want this syntax to work, you probably need to look into writing a custom version of the "for" tag.

Thanks,
Tim

Matias Pereira

unread,
May 22, 2012, 11:49:09 AM5/22/12
to dotliquid
Hi Tim,
Thank you for your answer. indeed, {{0 |}} getcategorybyid returns a
collection of a Class Categories.
I can do something like this?
{{ assign colllectioncat= 0|getcategorybyid}}
{% for category in colllectioncat%}
{{ category.name}}
{% endfor -%}

I was looking to change the Tag for, but I see complicated.
Thanks

Tim Jones

unread,
Jul 14, 2012, 3:39:07 AM7/14/12
to dotl...@googlegroups.com
Hi Matias,

Sorry for the late reply. Hopefully you got it working, but just in case (and for future visitors):

Yes, you could use the assign tag to do it. This works:

[Test]
public void Test()
{
Template.RegisterFilter(typeof(CategoryFilters));
string template =
@"{% assign colllectioncat = 0 | get_category_by_id %} 
{% for category in  colllectioncat -%}
{% endfor -%}";
Helper.AssertTemplateResult(" \r\nfoobar", template);
}

private static class CategoryFilters
{
public static IEnumerable<Category> GetCategoryByID(int id)
{
return new[]
{
new Category { Name = "foo" },
new Category { Name = "bar" }
};
}
}

private class Category : Drop
{
public string Name { get; set; }
}


Best wishes,
Tim
Reply all
Reply to author
Forward
0 new messages