[PHPTAL] Re: SourceResolver path in nested macro
> > ps. Something very different: is there a reference available to the > > Translator inside the templates, or do I have to provide one myself? (I > > can start a new subject for the archives if you want) > > Again, I appreciate your time, sorry for all the questions. > > I'm not sure what you're asking about. > > If you're writing your own translation service, then it might be easiest > to take PHPTAL_GetTextTranslator and modify it. > I've already written my own Translator Object and added it to PHPTal. I was wondering if I could access the Translator object in the templates by default, but nevermind. It's not really necessary, I'll leave you alone now. Thanks again... ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
Re: [PHPTAL] Re: SourceResolver path in nested macro
On 06-08-2010 at 14:13:19 Bas Kooij wrote: It seems a little complicated to have to output another macro to get it to work. Anyway, thanks for all your help, I've got it all to work the way it should. Still think it's pretty complicated stuff for an essentially simple operation, ie. parse some TAL variable twice. Yes, it is somewhat complicated, because PHPTAL is a compiled language. To be really fast it has to avoid parsing as much as possible. ps. Something very different: is there a reference available to the Translator inside the templates, or do I have to provide one myself? (I can start a new subject for the archives if you want) Again, I appreciate your time, sorry for all the questions. I'm not sure what you're asking about. If you're writing your own translation service, then it might be easiest to take PHPTAL_GetTextTranslator and modify it. -- regards, Kornel ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
[PHPTAL] Re: SourceResolver path in nested macro
Kornel Lesiński writes: > > On 06-08-2010 at 13:03:20 Bas Kooij wrote: > > > Sorry, I tried to simplify things by using a date tag, but this has just > > caused confusion. I don't just need to replace a date tag, I need to > > replace a > > tag named logos, which I'd like to replace with a macro containing TAL > > code to > > insert a bunch of company logo's. I have now done it like this: > > > > In home.xhtml > > > > > > > > > > > > [HOME] > > > > > > > > > > Then I use the following PreFilter: > > > > class HomePreFilter extends PHPTAL_PreFilter { > > function filter($src) { > > $content = [retrieve home page from db] > > $content = str_replace('[logos]', ' > macro="logos.xhtml/logos" />', $content); > > return str_replace('[HOME]', $content, $src); > > } > > } > > > > Which works fine. Is this the best way? > > For changing [logos] to > it's perfect. > > I'm a bit concerned about retrieving pages from DB directly in the > prefilter. Keep in mind that prefilters are applied to all files, so it > would be run on logos.xhtml as well. > > You could replace [HOME] with call to macro that is loads homepage via > resolver (prefilter will be applied to that resolved page too, so you can > have [HOME] that loads homepage with [logos] in it). > > As you've noticed, either way can be made to work, but in PHPTAL the > concept is that: > > * Load, but don't replace in resolvers. > * Replace, but don't load in prefilters. > Thank you, Now I let my database repository classes implement PHPTAL_SourceResolver. I do have to let the SourceResolver ouput a macro, right? So if I want to resolve this (to load page with pageId 5): I have to let the resolver output this: public function resolve($path) { if(path_starts_with("pageResolver_")) { $pagesId = trimleft($path, "pageResolver_"); $pageContent = [get page from db with $pagesId] $content = ''.$pageContent.''; return new PHPTAL_StringSource($content, $path); } } It seems a little complicated to have to output another macro to get it to work. Anyway, thanks for all your help, I've got it all to work the way it should. Still think it's pretty complicated stuff for an essentially simple operation, ie. parse some TAL variable twice. ps. Something very different: is there a reference available to the Translator inside the templates, or do I have to provide one myself? (I can start a new subject for the archives if you want) Again, I appreciate your time, sorry for all the questions. Regards, Bas Kooij ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
Re: [PHPTAL] Re: SourceResolver path in nested macro
On 06-08-2010 at 13:03:20 Bas Kooij wrote: Sorry, I tried to simplify things by using a date tag, but this has just caused confusion. I don't just need to replace a date tag, I need to replace a tag named logos, which I'd like to replace with a macro containing TAL code to insert a bunch of company logo's. I have now done it like this: In home.xhtml [HOME] Then I use the following PreFilter: class HomePreFilter extends PHPTAL_PreFilter { function filter($src) { $content = [retrieve home page from db] $content = str_replace('[logos]', '', $content); return str_replace('[HOME]', $content, $src); } } Which works fine. Is this the best way? For changing [logos] to it's perfect. I'm a bit concerned about retrieving pages from DB directly in the prefilter. Keep in mind that prefilters are applied to all files, so it would be run on logos.xhtml as well. You could replace [HOME] with call to macro that is loads homepage via resolver (prefilter will be applied to that resolved page too, so you can have [HOME] that loads homepage with [logos] in it). As you've noticed, either way can be made to work, but in PHPTAL the concept is that: * Load, but don't replace in resolvers. * Replace, but don't load in prefilters. -- regards, Kornel ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
[PHPTAL] Re: SourceResolver path in nested macro
Sorry, I tried to simplify things by using a date tag, but this has just caused confusion. I don't just need to replace a date tag, I need to replace a tag named logos, which I'd like to replace with a macro containing TAL code to insert a bunch of company logo's. I have now done it like this: In home.xhtml [HOME] Then I use the following PreFilter: class HomePreFilter extends PHPTAL_PreFilter { function filter($src) { $content = [retrieve home page from db] $content = str_replace('[logos]', '', $content); return str_replace('[HOME]', $content, $src); } } Which works fine. Is this the best way? Regards, Bas Kooij ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
Re: [PHPTAL] Re: SourceResolver path in nested macro
On 6 Aug 2010, at 12:16, Bas Kooij wrote: >> This is what prefilters are for: >> >> class DateMagic extends PHPTAL_PreFilter >> { >> function filter($src) {return str_replace('[DATE]', ' tal:content="date | nothing" />', $src);} >> } >> >> $phptal->addPreFilter(new DateMagic()); >> > > I tried usign PreFilters, but that doesn't seem work for inserted content, > only for code which is hard coded in the template. So if I retrieve the html > with the [DATE] tag from a database, insert it into a variable in my TAL > template, the PreFilter never fires for the [DATE] tag. If I put the [DATE] > tag in the template file itself it does work. That's correct. PreFilter is run only once before template is executed for the first time, and result of PreFilter is cached by PHPTAL for all later runs of that template. In PreFilters you're not supposed to replace your tags with data, but replace your tags with PHPTAL tags that will output desired data. For example if you wanted [DATE] to insert current date, use PreFilter with: str_replace('[DATE]', '', $src) and then only update date variable: $phptal->date = date('Y-m-d H:i:s'); This will give you tempalte that has [DATE] in it's source, and gives current date, updated every time. > I have a working solution now. I have this in the home.xhtml template: > > > > > > > > > > And I use the following SourceResolver: > > class MySourceResolver implements PHPTAL_SourceResolver { >public function resolve($path) { >if($path == 'homeMacro') { >$pageContent = [retrieve html with DATE tag from DB] >$content = ' macro="customMacro">'.$pageContent.''; >$content = str_replace('[DATE]', '19-02-1981', $content); >return new PHPTAL_StringSource($content, > PHPTAL_StringSource::NO_PATH_PREFIX.md5($pageContent)); >} >} > } > > Is this the easiest/only way to achieve what I want? You've reinvented prefilter here. It happens to work because md5($pageContent) forces to reload whole template whenever date changes (that's quite inefficient). Even if you wanted template recompiled like that, you could do it with prefilter: class DateMagic extends PHPTAL_PreFilter { function filter($src) {return str_replace('[DATE]', '19-02-1981' , $src);} function getCacheId() {return '19-02-1981';} // http://phptal.org/manual/en/split/prefilters.html } Do you want to allow your clients to use all PHPTAL constructs, or only HTML with your tags? If the latter, then you don't need any resolvers or filters: $phptal->page_content = str_replace('[DATE]', '19-02-1981', retrieve_html_with_DATE_tag_from_DB()); -- regards, Kornel ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
[PHPTAL] Re: SourceResolver path in nested macro
> > > Slightly off topic... > > > > I really love PHPTal, it has cut development time for implementing templates > > in half. > > The only time I get stuck is when I have to insert dynamic templates into my > > PHPTal template files. This is quite a common occurrence when clients use a content > > management system to edit pages. I want them to be able to insert some sort of > > tags in the CMS, ie. [DATE], which I can then replace with PHPTal code, for instance > > . > > This is what prefilters are for: > > class DateMagic extends PHPTAL_PreFilter > { > function filter($src) {return str_replace('[DATE]', '', $src);} > } > > $phptal->addPreFilter(new DateMagic()); > I tried usign PreFilters, but that doesn't seem work for inserted content, only for code which is hard coded in the template. So if I retrieve the html with the [DATE] tag from a database, insert it into a variable in my TAL template, the PreFilter never fires for the [DATE] tag. If I put the [DATE] tag in the template file itself it does work. I have a working solution now. I have this in the home.xhtml template: And I use the following SourceResolver: class MySourceResolver implements PHPTAL_SourceResolver { public function resolve($path) { if($path == 'homeMacro') { $pageContent = [retrieve html with DATE tag from DB] $content = ''.$pageContent.''; $content = str_replace('[DATE]', '19-02-1981', $content); return new PHPTAL_StringSource($content, PHPTAL_StringSource::NO_PATH_PREFIX.md5($pageContent)); } } } Is this the easiest/only way to achieve what I want? Thanks for your time, Bas Kooij ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
Re: [PHPTAL] Re: SourceResolver path in nested macro
On 6 Aug 2010, at 10:35, Bas Kooij wrote: > Thanks for the reply, > > So if I name the macro customMacro/customMacro the resolver would recognize > it? But that makes the second part irrelevant for CustomResolvers right? Right. If you use path customMacro/customMacro then resolver will get "customMacro" as path to resolve. Macro syntax is path/macro, and resolver is only for resolving paths, not macros. If you used macros "path/macro1", "path/macro2", "path/macro3" then your resolver would be called only once with path "path". > Slightly off topic... > > I really love PHPTal, it has cut development time for implementing templates > in half. > The only time I get stuck is when I have to insert dynamic templates into my > PHPTal template files. This is quite a common occurrence when clients use a > content > management system to edit pages. I want them to be able to insert some sort > of > tags in the CMS, ie. [DATE], which I can then replace with PHPTal code, for > instance > . This is what prefilters are for: class DateMagic extends PHPTAL_PreFilter { function filter($src) {return str_replace('[DATE]', '', $src);} } $phptal->addPreFilter(new DateMagic()); -- regards, Kornel ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
[PHPTAL] Re: SourceResolver path in nested macro
Thanks for the reply, So if I name the macro customMacro/customMacro the resolver would recognize it? But that makes the second part irrelevant for CustomResolvers right? Slightly off topic... I really love PHPTal, it has cut development time for implementing templates in half. The only time I get stuck is when I have to insert dynamic templates into my PHPTal template files. This is quite a common occurrence when clients use a content management system to edit pages. I want them to be able to insert some sort of tags in the CMS, ie. [DATE], which I can then replace with PHPTal code, for instance . The way to go for this at the moment is using CustomSourceResolvers, which are not entirely trivial to implement. Would it be a bad idea to implement some sort of custom keyword which would allow inserting code into the template? What if you could write the following in your PHPTal template: Where the cmsContent would be a TAL variable containing more TAL containing more TAL code. This way I could retrieve the HTML with tags from the database, replace tags with TAL code and insert it into my templates very easily. Sorry for the rant, I know all this can be achieved at the moment. I'm just throwing an idea out there. Regards, Bas Kooij ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal