SugarNewsWikiDocsBugsForumFilesSource LoginRegister
HomeForumsSugarsections, inserts, and layouts
sections, inserts, and layouts
Sugar
Sean Middleditch Nov 22nd, 2009 at 5:29pm
Subversion Sugar now supports sections, inserts, and layout. These are a pretty cool feature set found in a lot of non-PHP HTML templating systems. The primary feature is layouts. A layout is a secondary template that is used to contain or wrap the the primary template. A layout would include the leading HTML tags, head tags, and so on, and then somewhere in the middle has a directive {% insert content %}, then followed by the footer data for the site. The main template is automatically inserted as the content. This is a lot easier to use than including header and footer templates in every page, and its far more flexible as it allows changing the layout separate from the page very easily for theming purposes.

However, layouts by themselves are not quite flexible enough. Individual pages often need to override bits of the layout, such as the page tile, navigation bits, CSS/JS includes, and so on. This is where sections and inserts come in.

The layout template can define a section with a given name. Each section is a block that contains some HTML/Sugar content in it. However, these sections are not immediately evaluated! They are compiled and stored away for later use. For example,

{% section page_title %}Default Title - {% $site_name %}{% /section %}

The layout can then specify an insertion point for the section, using the syntax shown above for inserting the main content:

<title>{% insert page_title %}</title>

Here's the neat part. The primary page template can also define and redefine sections. So a catalog page template might have a section definition like:

{% section page_title %}Products &amp; Services{% /section %}

When the combined layout and page are evaluated, any sections in the page will override sections in the layout. The end result is that the content placed at the page_title insertion page is the content from the page (Products & Services) and not the default content defined in the layout. Pretty damn useful.

The final trick to note is that the page template's body is automatically defined as the section named content (which is why insert content will insert the page body). However, the page body can explicitly redefine the content section, allowing page templates to be (IMO) more nicely formatted as a list of sections. It makes the template structure feel a little more XML-ish.
Sean Middleditch Nov 22nd, 2009 at 5:33pm
Forgot to mention that there are a few additional enhancements in store for layouts and sections, mostly for convenience factors. First, I would like sections in the layout template to automatically be insertion points. It's a little annoyoing to have to define a section and then immediately add the insertion point, which is almost always what you wanted to do.

I may just make any section block in a layout template automatically be an insertion point, or I may take a coworkers advice and support a directive like {% section insert page_title %} ... {% /section %} that combines the two. Haven't decided yet.

I also may allow sections to have a short-hand for when the section is short and simple, like page titles, where it may be easier to use a syntax like:

{% section page_title = $page.title %}

instead of:

{% section page_title %}{% $page.title %}{% /section %}

(Sure, you can replace the %}{% pairs with ; but that's not all that much shorter.)

Reply to Topic
or Login