Title: Is there a smarter way to implement template inheritance in RTC Post by: xin.chang on August 27, 2017, 12:27:47 PM I haven't find any demo in RTC to implement template inheritance, so I did that by:
1. define master template file(master.html) like this: <html> <% head %> <% body %> </html> 2. use a config file to describe relationships, for instances: child.html: master_template=master.html head=head.html body=body.html 3. process all variables(head, body), read the configuration, then parse "block-template" files separately, and fill them to variables in master template at last. I want to ask is there a smarter way to do that, or will RTC support template inheritance/template include in the future? Title: Re: Is there a smarter way to implement template inheritance in RTC Post by: D.Tkalcec (RTC) on August 27, 2017, 01:04:14 PM Since templates don't contain any business logic, but are only flat files with place-holders to be filled up in code, your solution to combine multiple template files into single output is as good as any.
If you are looking for more examples on using the TRtcParse class (implemented in the rtcParse unit), check the RTC_WebPackageManager and/or the RTC_WebForum Demos. Both of these Demo Projects make extensive use of the TRtcParse class to generate dynamic content for the Web using template files. Best Regards, Danijel Tkalcec Title: Re: Is there a smarter way to implement template inheritance in RTC Post by: xin.chang on August 28, 2017, 04:26:26 PM RTC_WebPackageManager and RTC_WebForum are really good examples, that I had learned a lot from.
What I want to achieve is, my web server could process any template file(which maybe include another templates by read them into placeholders, or inherits another template by fill itself into placeholders of parent template) without hard-code. template include: Code: <% include('header.html') %> I can use TRtcScript to implement(create a function "include", just load it and return the content): Code: <? include('header.html') ?> template inheritance: master.html(pseudo-code): Code: <html> child.html(pseudo-code): Code: <% inherits('master.html') %> Like you said, template is just a plain file with placeholders at present, so it's hard to do the block replacement. So I describe the relationships like I mentioned: Code: <html> Code: master_template=master.html But the problems: 1. the placeholder is not a block, so it could not have default content. 2. must break all blocks into many child_X template files(hard to maintain), instead of write the content of which block will be filled with in one file. 3. it's not easy to do multilevel inheritance. I wonder is there any possible to provide extra events/methods or metadata for TRtcParse, so I could do the block replacement trick. Any advice and suggestions will be greatly appreciated. Title: Re: Is there a smarter way to implement template inheritance in RTC Post by: D.Tkalcec (RTC) on August 28, 2017, 05:53:16 PM Actually, after taking a closer look at the TRtcParse class implementation (it's an old class which I haven't really looked at for quite a while), I've found that it already has (undocumented) support for the <%INCLUDE:filename-to-load%> tag, as well as conditional branching using the <%IF:condition-variable%> and <%ELSEIF:condition-variable%> tags, which are both being used by the RTC_WebForum Demo (see the "index.htm" file), but ... since the logic required to fill this "combined" template file still has to be written in Delphi code (where you are creating the TRtcParse instance and filling up the template), I find its usefulness rather limited.
As for your "inheritance" suggestion, I'm affraid you've completely lost me there. Events or metadata are probably the last things I'd want to add to a simple template parser. In fact, the TRtcParse class is already doing what it was meant for and I have no plans of extending it beyond that. But ... since the TRtcParse class is NOT used by anything else in the RTC SDK, if you feel like adding more to it, you can always write your own (more advanced) version by using the TRtcParse class implementation as a starting point. Just create a new unit and give your class a different name, to avoid naming conflicts. Best Regards, Danijel Tkalcec Title: Re: Is there a smarter way to implement template inheritance in RTC Post by: Dany on August 30, 2017, 03:20:25 PM I don't know if it will help you, but i have some places with two levels. What i do is i run it through the parser (TRtcParse) once using one token (TokenOpen, TokenClose pair). In the files there are other tokens. Then i can take that result and run it through a parser using the other / another token.
Now, since you mention inheritance, this may be unmaintainable for more than two or three levels. Perhaps you can figure out some smart way of preprocessing the templates though. HTH, /Dany Title: Re: Is there a smarter way to implement template inheritance in RTC Post by: xin.chang on September 01, 2017, 07:46:39 AM Thank you both. You guys inspire me a lot.
|