5

In order to maximize code reuse, I'm trying to break down the mark up for each of my form fields into individual "containers" (e.g. a template, snippet, global). The project I am working on is form-heavy where the same field is used multiple times in different pages. The fields are part of forms that insert and edit Channel data.

Where is the best place to store these pieces of HTML? I tried snippets and globals, but I think EE's parse order does not allow Channel data to be displayed in these containers. If I used templates, that's going to be a lot template calls.

I'm using Safecracker, but not sure if that matters here.

Suggestions?

7

As someone who has been using EE for years, and going about templating in exactly the manner you are trying to for those many years, and who is just now learning a much easier way, I would like to suggest you look into using Stash.

The difference is that in the EE templating method you are pursuing, and that I and many others used for years is that your markup is spread throughout so many difference templates, snippets, global variables and more. The idea with Stash is that you have one (maybe two for very complicated sites) wrapper template.

I create one template in a template group called wrappers, then I do a single embed of this template in each of the actual site templates I create.

{!-- Embed the Primary Template --}
{embed="_wrappers/_main"}

Then, by using Stash, you can “inject” content from the template you are loading via URL into the wrapper template that is embedded.

Setting Content:

{exp:stash:set name="stuff"}stuff goes here{/exp:stash:set}

Calling set content in the embedded template:

{exp:stash:get name='stuff'}

When you really start exploring the possibilities of this method, a whole new world opens up. All your primary code is or can be stored in one template. The code that changes from template to template is stored in those templates.

Just to give you an idea of the possibilities, I don’t just use Stash to set content in the wrapper template, I use it to set certain variables too. Using the excellent Switchee (you can use {if} statements as well), I can control various aspects of the wrapper template. For instance on the site I’m working on right now, the client wants a sidebar to appear on some pages, on others, not. But in most respects is simple enough to use only one wrapper template. So I’ve set up Switchee case logic to turn the sidebar on or off by getting the value of one of my Stash sets.

{!-- Show the Sidebar? Possible Values are: "sidebar" "nosidebar" --}
{exp:stash:set name="template_type"}sidebar{/exp:stash:set}

The reason I’m bringing this up is because I wish I had know about this method sooner. It makes templating so much easier, and makes it a lot less of a hassle to keep track of where everything is.

There's value in tempting with snippets and global variables, but going this route with Stash is the method I always recommend using now if at all possible.

I still use Variables and Snippets to be sure, but not very many.

2

My suggestion would be to use Low Variables, which allows you to set parse order for your snippet effectivly letting you set it to be a global variable or a snippet. Additionally you can pass variables to a low variable as if it were an embed using preparse (unlike traditional snippets which can't have anything passed to them) - see this blog post for details.

Bonus points for Low Variables saving your variables as flat files for easier editing.

1

See this post for some good answers about the different options available in EE for fragment reuse, but fundamentally if you want early parsing (so you can have the template tags in your fragment parsed) you normally want a snippet. However bear in mind that these cannot be nested. Embeds can, but these as you've noticed come with a hefty performance cost.

If you have more complex needs in this area than the native functionality can provide, have a look at Low Variables, and (particularly for getting around parse order issues) Stash.

  • are snippets really an option for me? These form fields contain Channel field tags (e.g. {first_name}). When I tried snippets, the tag instead of the entry's field value. – ExpressionEngineNewbie Dec 21 '12 at 13:11
  • I guess my confusion is about snippets being able to contain "dynamic content." Doesn't that mean Channel entry's field values? – ExpressionEngineNewbie Dec 21 '12 at 13:22
  • I rarely use snippets, only if I need the CP to update the values. I use Stash for everything, it's one of the best EE add-ons in my book. I can't recommend Stash enough. – Justin Kimbrell Dec 21 '12 at 13:28
  • 3
    @JustinKimbrell I'd agree that stash is super powerful, but it's such a different approach to native templating that it's not where I'd suggest someone new to EE starts. I think you need to have a good understanding of the problems Stash solves (esp the parse order) to use it effectively. – Tom Davies Dec 21 '12 at 13:50
  • 2
    @ExpressionEngineNewbie yes, you definitely can use custom field tags (or entire channel:entries loops) in a snippet. A snippet is basically an early parsed string replacement in your template, so if it works in the template it should work when pulled out to a snippet and referenced (assuming you're not doing anything crazy like nesting them). – Tom Davies Dec 21 '12 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.