formosatek/dotliquid — Issue #50: Allow any types, without ILiquidizable?
Author: gregmac
Created: 2012-01-31 07:00:24+00:00
State: open

I am trying to pass something like the following object to a view:

```csharp
var data = new {
Model = GetMyData(),
BaseUrl = "/something/";
};

public IEnumerable GetMyData();
```

The trouble is, due to the requirement to have ILiquidizable on all types, I get the error: `Liquid syntax error: Object 'Models.MyData' is invalid because it is neither a built-in type nor implements ILiquidizable`.

I understand this is by design, but it would be really nice to have the ability to override this restriction if I know that I am passing it a safe-to-use model. This is really annoying since I am specifically using DTOs already, and essentially the only way to get this to work is to implement ILiquidizable and return a new anonymous type that mirrors my actual model.

I tried using `object DotLiquid.ILiquidizable.ToLiquid() { return this; }` but that also does not work -- it just silently fails, as if the variables I'm referencing don't exist.

I can picture a couple of ways to do this:

```csharp
// as a global setting
DotLiquid.Template.AllowAllTypes = true;

// as a wrapper:
Hash.FromSafeType(model);

// as an override
template.Render(DotLiquid.Hash.FromAnonymousObject(model, allowAllTypes:true));
// or
template.Render(DotLiquid.Hash.FromAnonymousObject(model), allowAllTypes:true);
```
The actual implementation is pretty easy once the method is chosen -- it's a matter of modifying Context.Liquidize().

I will submit a patch if needed, but want feedback and alternatives before I do.

(aside, the page linked in the docs of ILiquidizable -- http://wiki.github.com/tobi/liquid/using-liquid-without-rails -- is gone. It appears to be https://github.com/Shopify/liquid/wiki/Using-Liquid-without-Rails/cadb5d13cb171d36b4aa9239af1b8b70bc699ad1 but I can only find this referenced by search, the current version of that page is also missing?)