Re: Getting access to parent model

Chris <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Oops, multiple errors in this message. Please disregard.

First, there's junk at the bottom of the messsage I didn't intend to send.

Second, adding parentheses fixes one problem: ${my_variable.asDate()}

Third, since the parent model is the one creating the AsDate method 
class, it can just pass itself to that class in the constructor. Duh.

Everything works now. Never mind...


On 1/7/2011 5:13 PM, Chris wrote:
> I'd like to be able to do something like this in a template:
>
> // shows raw text
> ${my_variable}
>
> // converts to a date type
> ${my_variable.asDate)
>
> // runs some other arbitrary function on my_variable
> ${my_variable.asSomethingElse)
>
> I got it to work (partially) by making "my_variable" a class that
> implements both TemplateScalarModel and TemplateHashModelEx. I can get
> both ${my_variable} and ${my_variable.whatever} to return scalars.
>
> Now the tricky part: I need to return the hash values as objects that
> implement TemplateMethodModelEx, and those methods need to get access to
> the text of my_variable.
>
> Unfortunately, Freemarker throws an error when it sees the
> TemplateMethodModelEx ("Expecting a string, date or number here"), and I
> haven't figured out how to get access to the parent model from within my
> method so I can get the text value of "my_variable".
>
> Is what I'm trying to do possible?
>
>
>
>
>
>
>
> I think I could implement the bottom two by making my_variable a
> TemplateHashModel and passing the string value to be converted to it.
>
> The trouble is the top one. FreeMarker throws an exception rather than
> just calling toString() on it. ERROR: Error on line X, column Y in
> /results Expecting a string, date or number here, Expression item is
> instead a com.MyHashModel.OrWhatever.
>
> I'd be happy to do it using ? syntax instead: ${my_variable?asDate}, but
> you can't create your own builtins (according to our exchange on this
> topic Dec 9, 2010).
>
> Is there any way to
>
>
> ------------------------------------------------------------------------------
> Gaining the trust of online customers is vital for the success of any company
> that requires sensitive data to be transmitted over the Web.   Learn how to
> best implement a security strategy that keeps consumers' information secure
> and instills the confidence they need to proceed with transactions.
> http://p.sf.net/sfu/oracle-sfdevnl



------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web.   Learn how to 
best implement a security strategy that keeps consumers' information secure 
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.