Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Template::Toolkit - How do i access alias-based results?

by 1nickt (Canon)
on Jul 13, 2015 at 23:07 UTC ( [id://1134605]=note: print w/replies, xml ) Need Help??


in reply to Template::Toolkit - How do i access alias-based results?

Hi Yaerox, glad to see you experimenting with TT2.

I read most of this thread and here are some observations:

In the first example code, once you have aliased your table as u you can just refer to it as that.

I disagree with the monkm who recommended not using TT2 because of its complexity. It is true that it will require more time to learn, but at the end you will have a more powerful system.

I strongly recommend not running DB perl code in your template. Sure you can, but why in the world would you? You are still obviously going to have a perl program that uses the template; that's where you should be doing the Perl work.

An exception could be when you can retrieve a set of records with a one-line dbh call and then feed them to a TT loop. But when you are going to be processing the data or it's a multiline SQL statement, there is no way that should be in the template. That is the whole point of the template! To separate your code languages.

Another exception could be that you retrieve the records with DBI in your Perl program and pass the record set into the Template where a Macro might process some format or something like that, or look up a string in a language library. In some cases the greater benefit is in keeping the Perl code clean.

Typically, however, I have always tried to do every bit of processing I could in a Perl program, building up a highly complex data structure of hashes of arrays of hashes of ..... and then passing that in to the template as much intact as possible.

HTH

Remember: Ne dederis in spiritu molere illegitimi!

Replies are listed 'Best First'.
Re^2: Template::Toolkit - How do i access alias-based results?
by Your Mother (Archbishop) on Jul 14, 2015 at 14:10 UTC

    Again, I'm a TT fan, contributor even, and I will continue to use it in personal projects but probably never again at work. The issue isn't that TT is too powerful exactly. The issue is the power makes templates harder to understand and maintain for others and encourages bad behavior in devs, exactly as you say–

    Sure you can [run DB perl code in your template.]

    I'm not quite a View purist and it sounds like you personally do things Right®. Something like Text::Xslate almost necessarily leads to simpler templates. It's also much more strict, which leads to better practices and easier maintenance. Just to say it again, TT2 really is 1,000x slower than Xslate in some use cases and there are one or two other template engines that are also in the class of zippiness. I know template speed is the last on the optimization checklist but if the difference is 500 renders per second instead of 5, and you're in front of nginx or something which is capable of handling much more, it matters.

    I don't discourage TT use out of ignorance but out of thorough familiarity.

Re^2: Template::Toolkit - How do i access alias-based results?
by Yaerox (Scribe) on Jul 14, 2015 at 06:51 UTC
    Thank you for your reply sir.

    Exactly what you said, is what I was thinking about all day long. I mean using SQL in Perl or on my Tempaltes do infect performance too, and I wanna go with TT because I wana separate code of all my languages. That was the reason why I asked for help doing it like this once. Now that it works, I'll go te way you described here.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1134605]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-24 09:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found