Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Is there a way to store a part of template in db field and render this?


Reply


7 Posts   510 Views

Avatar
k0m0r

Community Member, 39 Posts

15 February 2012 at 1:47am

Hi.
I'm wondering if I could store a part of the template inside a DB Field. I want to allow my CMS users to insert part of the Page template directly through CMS, for example the Page template would be:

<html>
<head>
....
</head>
<body>
<% include Header %>
$UserCode
<% include Footer %>
</body>
</html>

and UserCode would be a DB field, with text containing template controls, like <% control %> tags and field names, for example:

<h1>Hello World</h2>
<h2>$MenuTitle</h2> <!-- or Parent.MenuTitle maybe? CurrentPage.MenuTitle ? -->
<% control Images %>
<img src="$URL" alt="" />
<% end_control %>

And now, when calling 'renderWith', I would like the UserCode content to be rendered as a part of template. So basically it should work just like <% include %>, but not stored as separate files in Includes folders, but directly in DB, managable through CMS.

Is this achievable?

Thx a lot.

Avatar
martimiz

Forum Moderator, 1107 Posts

16 February 2012 at 1:09am

Edited: 16/02/2012 1:10am

Maybe this would work:

$content = DataObject::get('MyTemplate');
$result = SSViewer::parseTemplateContent($content);

But I'm not sure about this - normally all template files get cached in the silverstripe-cache to avoid overhead. That wouldn't work for templates in your database though... So you could consider writing to file instead.

Having said this - what would be your reason for having your templates in the database? It could be a hughe risk to the stability of your site, if people don't know what they're doing. Or if the server hicks-up while saving. And the overhead...

Avatar
k0m0r

Community Member, 39 Posts

16 February 2012 at 10:22am

Edited: 16/02/2012 8:51pm

Hi.
Maybe describing of what I use silverstripe for would help in understanding what I have to achieve.

I have a site with hundreds of static pages, each having 90% of the content identical. Site structure and the reccurent content are managable through CMS. The point is, that the remaining 10% is almost always a separate piece of HTML, containing template controls.
And SS is used here not as a live site manager - I export every page to a static HTML file, so basically SS is only needed to automate some work that my developers usually do. Inserting a part of HTML containing template controls through CMS is far more simple than creating a page class each time, whatsmore - this way they don't need access through FTP and I can easily track which one of them made the changes in versions history.
So I don't care how much this bothers the site behaviour, because the only effect I need is a bunch of exported static files.

I'll give your suggestion a try and post my results here.
Thanks a lot.

Avatar
k0m0r

Community Member, 39 Posts

16 February 2012 at 10:26am

Oh, and I forgot to mention that some of these pages could hit half million views per day, so exporting them to static is my only option, I don't suppose SS could deal with such a high traffic.

Avatar
Mo

Community Member, 510 Posts

21 February 2012 at 12:19am

Not sure exactly what you are doing, but if you are using a Page class (or descendant of) then you should be able to use the standard datamodel and the onBeforeWrite method.

So, for example, in your Page.php class:

class Page extends SiteTree {
public static $db = array(
'CustomHTML' => 'HTMLText'
);

public function onBeforeWrite() {
$this->CustomHTML = '<div class="something"></div>'; //Add your custom HTML as a string or from another source.
}
}

Not sure if that is what you are after?

Mo

Avatar
Mo

Community Member, 510 Posts

21 February 2012 at 12:25am

Also, komor,

Silverstripe's caching engine is actually pretty good, apparently running it through Windows server and using its built in caching features is very efficient. You can also do some very aggressive caching using apache. The trouble is you will still have transactions going to and from your database whatever you do, and in the end the only other solution would probably be to add more servers and use/improve load balancing.

But if you are saying that the vast majority of your content pretty much never changes, then I am pretty sure that generating static pages is the most efficient form of serving content.

Mo

Avatar
martimiz

Forum Moderator, 1107 Posts

21 February 2012 at 1:21am

Edited: 21/02/2012 1:22am

First of all you don't really have to create a new pagetype every time you want a new template. SilverStripe works well with include templates. Either write some conditional logic into your main template - or add a dropdown to the CMS to dynamically select a template to include, and store only the templatename. The important thing is that the current pagetype holds everything the template needs - if not, you'll have to create a new pagetype anyway!

If you reeeeally want to 'design' your templates from within the CMS, you could consider writing them to file. I would normally be extremely apprehensive about it - one typo would crash the site. And you'd have to flush to make SilverStripe see it.

Because you say you don't need the templates to be cached, since you'll only ever use them to create the static page (using StaticPublisher?), I guess your idea might work better. You'd use the SSViewer class to process your template. See ViewableData::renderWith() for hints on how SilverStripe does this. Why do I still feel slightly nervous about this? :-)