Where to store flat files in CakePHP? - templates

I am building an application that allows a user to browse html templates. The html template will be displayed in a preview area (source code will be obfuscated), and although the template will contain inline CSS/JS, there will also be the odd linked image asset.
For this reason, I thought it might be best for me to just store them as a library of folders, and retrieve the flat file from my server for display, as opposed to storing the HTML in the database and having to organise the external assets too.
My question is: Where in the CakePHP directory structure, is the best home for these files? I'd rather keep them above the webroot, and I'm thinking View.
Any ideas? Thanks.

I would suggest that you keep all the out-of-cakephp-framework related files, stored under the webroot.
My suggestion is the following directory structure for saving the html template as well as the css and js related files.
/webroot/files/templates/<template-project-name>/<the-actual-file>.html
Any external css, would then be under
/webroot/css/templates/<template-project-name>/<the-actual-css>.css
And the JS libraries would be under
/webroot/js/templates/<template-project-name>/<the-actual-js>.js
Use a seperate layout file (call it preview_layout.ctp) which simply renders
the template file as it is.
Hope it helps

Why do you want to store them above the webroot, when they are publicly-viewable assets? My gut feeling is that they really belong in the webroot or in the database. I would pick one or the other.
If you must store them above the webroot, I guess the best thing might be to store them as elements.
app/View/Elements/your_templates/
And that way, you can load it into the preview area of your View using:
echo $this->element('your_templates/template');
Edit:
Honestly, the suggestion of elements is not what I would recommend, I'm just trying to offer you different suggestions. It would be terribly bad practice and I think it will be more messy and problematic than it's worth.
As for the image files, put them in the webroot, regardless of where you choose to store the HTML templates. You can't stop people downloading the images once they are viewing them in their browser anyway.

Related

What is the best way to fin and use a template in cakephp 3

i'm new in cakephp and I have started with version 3. I want to build a beautifull app and because I'm not good in design, I would really like to use a free template or buy one that I can use within cakephp.
So, I would really appreciate all your propositions and ideas or best practises. The easy way will be the best because I don't have a lot of time with this project. Thank you in advance.
If you don't have a lot of time like you mentioned, the easiest way to go ahead and get started is to paste a lot of the code in your default.ctp layout inside of src/Template/Layout/default.ctp.
You'll notice there are some lines of PHP already in there that are relevant to fetching blocks of css, meta tags, and other bits of code that could potentially exist throughout your project.
Find the main layout of the theme your trying to use - the one that will be consistent across most of the pages. That's the one you'll use for default.ctp. Compare what's already in default.ctp and make the comparable adjustments around the HTML in that document while keeping the important lines of PHP there as well.
For other important pages like a login or registration page, just create a new document for those, like 'login.ctp', then inside the function that loads the page (maybe 'login' inside of UsersController'), change the default layout with this line of code:
$this->viewBuilder()->layout('login'); // without the .ctp ending
This way you can create one-off layouts that don't really match any other page.

Change Joomla Template parameters

Im developing an e-commerce with Joomla 2.5 using a template which has a slideshow embedded (not a module).
I want to create an article where the client himself can easily upload images and their descriptions and urls for the slideshow.
I've managed to load a file uploader module in an article and it works. But I don't know how to change the descriptions and urls.
The descriptions and urls are template parameters and I don't know how to set them from the article.
The params.ini file in template folder is blank.. The .xml shows the 's for them but not their current value.
You are getting two functions to work, but you're missing the whole logic that links images to pages, and assigns them names, possibly manage a little security.
Why do you want to build this from scratch? You'd be so much better off with a ready-made, free, tested and supported CCK (e.g. contentbuilder, k2, sobi, zoo just to name a few).

Include a page in the template script in magnolia cms

How can I include a page from magnolia into a magnolia template script?
In the template script with I can access data from a specific page, but how can I load that page into the template?
Let's say I have 2 pages each with its own template. Page 1 contains in its tree page 2. I want to include in the template script of page 1, page 2, but doesn't work.
Thank you very much :)
UPDATED
What I actually want to do is include my header in all of my project's pages. But I don't want to put it as a paragraph, because if I ever want to change my header, I'll have to edit all the project's pages.
So what I try to do and I don't know if this is the correct approach is to create a page template for the header. This template won't include any , or css, it's just the code for the header.
The next thing I want to do is create a page in magnolia with that model to be the header.
Next I'd like to include the page I've just created in my main template model for the project, but I can't figure how to do that.
I am new to Magnolia cms and initially I tried creating my demo site using stk. The only problem was that I couldn't use jsp as a scripting language, or at least I couldn't find any solution on the internet. I don't really know freemarker, but that's not really a big problem. I'm really reluctant in using freemarker because maybe in the future in a more complex project I might need some features that freemarker doesn't support, but jsp does. I'd like to build my site using jsp if that's possible with magnolia.
I'm sorry for this long update, but if anyone has any suggestions on what a best practice could be and if I could implement what I want in jsp I would be really grateful.
Thanks again for you time :)
If you're using the STK then see this guide on content-reuse.
If not have a look at the cms tag-lib, especially the tags cms:loadPage and cms:setNode with which you can get a piece of content and set it as a JSP/JSTL variable and then render it using cms:includeTemplate.
A common scenario is to 'inherit' content from the parent page, the header is an excellent example of this. What you do is for an area you walk up the content hierarchy and render everything from the parent pages in their area with the same name. This way the header which you've only added to the top page is included in all its children.
Another option is to a have special page which simply holds things to be included in other pages. Like header, footer and banners that should go in the side pane of some pages.
Including a page within another page doesn't really work. Page 2 already has its own <html> tags, its own <script> tags, and its own CSS, so including it wholesale into another page just simply doesn't make sense.
You do, however, have a couple of options:
Use an iframe. This will allow you to include the entirety of Page 2 in a region of Page 1.
More recent versions of Magnolia will allow you to render an individual paragraph, which you could then include in another page. (For example, you can see a single paragraph from http://demopublic.magnolia-cms.com/demo-project/about/subsection-articles.html at http://demopublic.magnolia-cms.com/demo-project/about/subsection-articles/article/main/0.html.) This requires knowing a bit about the way the data is structured, but is a pretty useful way to be able to selectively extracts bits of a page.
You can use the Magnolia API in your model class to pull data from sub-pages, and then make it available to your view template. This is the approach the STK uses to build teasers that include content from the pages they reference, and is probably the most powerful and flexible approach, but it does require actually writing some Java code. (See http://documentation.magnolia-cms.com/templating/stk/templating.html and http://documentation.magnolia-cms.com/reference/templating.html for details of how to use this approach.)
(Added after question was edited) The footer functionality that's included with the STK does almost exactly this. You might be interested to take a look at that and see how it's implemented there.
Hope that helps a bit!

Storing Django translations in a database

I am working on a Django application which is going to be deployed to Heroku (on their Cedar stack). A limitation of this is that we cannot write to disk. However, Django translations reside in .po files, and therefore the client will be unable to change the translations on the live site without involving a developer (which is not ideal).
The alternative solution therefore seems to be to store the translations in either an RDBMS, Mongo, Redis etc etc.
Is there any sensible way of achieving this? Is it even a good idea? (I wouldn't want to hit the DB for every translation!)
Edit: There seem to be lots of Django apps out there for translating text which is stored in a DB, but not for actually storing the translations themselves in a DB.
What I think about Django translations and translations stored in the files in general - this is for the parts which will not change and is not dynamic. Like constants you have in the website. And if you have dynamic text, which could be and must be edited when the website is running - I would say this is the same thing as normal content (blog entry, comments and so on).
So you can just develop simple module "site parts", with the template tag, which will grab the right thing for you from DB. Like from template you can call { get_site_part example }. And then you can edit those parts from admin interface.
I would not recommend to store dynamic content to the system files. If you worry about hitting database every time you need this - then caching should help here also you can develop smart template tag and grab all the site parts you need in one query. Then it will be nothing different from just simply loading blog entry or the comment for it.
Maybe I'm wrong, so this is just my 2c on this topic :)
Ignas

How to ensure that a javascript file is included only once in Django

I have a few child templates which have extraheads which include the jquery script. Sometimes they are used together. How can I have the jquery javascript file loaded only once? If it were convenient to set template variables, I could set and check one before including the line.
My advice: Just include jQuery on your base's <head> and call it a day. Saves you from having to worry if a child template uses jQuery or not and it is just a 19kb download on the first page load and that's it. If you use Google's API cloud, it may not even be any as the user might have it cached from another site.
This may not work for you, but I advice you to consider it if possible.
My usual approach to this problem is to either wrap all of my child templates in one template that takes care of my includes (JS and CSS). I then make sure my caching is set properly, so that these scripts are only downloaded once per user. In other words, I force the download of all my external scripts on the first view, then rely on caching to not redownload the JS each time.
Combining all of your JS into one file will also improve download time due to the reduction in requests that will be generated.
Another thing to note is that you mentioned putting the JS in heads. While most people do this, placing JS in the head can make your pages appear to load slower. JS files are not downloaded in parallel, so they block all other downloads. Google and Yahoo recommend placing JS at the bottom of your page where possible to improve the user experience.
See the Yahoo YSlow tool and the Google PageSpeed tool for this.
I'd mostly bite the bullet and load jQuery with every template. But if you really really really have to have this feature, then I'd recommend a custom template tag. Check out the docs, especially the part about setting a variable in the context.
You could define separate blocks for your script and for other extraheads in base template.
In base template leave block for your script blank. Fill it with link to a file when needed in templates which are extending base.
You could pass all required files as a context variable and then write a template tag that removed duplicates and loaded what was left.
You could control load order this way also.