I want to write a program with Yii so that users can have their template.
My solution is that every user has a record in a DB to keep a customized template like HTML code and some syntax like Smarty or Twig.
How can I use PHP to get that records and just print it on the screen and my template engine extracts syntax and shows best results? Is that possible or not? How should I do this?
Really you know if you save php codes in database, when you print them into a file, php codes you saved in database don't run. For me the most important thing is that to save template in database whit php commands and then echo them in a file and php code run same usually.
But is is important to use smarty or twig and don't let users add php codes directly.
I found my answer. Just save every thing in database and when load them use eval() function. See documents in php and w3school.
For user interface templates you can use different layouts in Yii. Just put your different layout templates in the ./views/layouts
folder:
./views/layouts/main-template-1.php
./views/layouts/main-template-2.php
./views/layouts/main-template-3.php
and select the layout in the controller when executing an action depending on the user which should be known in the controller.
EDIT: Without uploading layout files from users you can load the template code from the database (HTML skeleton) and then in the Controller put together layout and view code:
$contentWithoutLayout = $this->renderPartial('myview',...);
return $this->applyLayout(contentWithoutLayout, $layoutFromDb);
where applyLayout()
is your method to merge layout and view content.