String Plugin Code Examples

Here are some code examples for common uses for the string plugin to get you started. Many of these examples come directly from the templates we use on this site.

Note: in each of the examples, "main template" means the template that gets called initially by ExpressionEngine for a particular page. Assuming you're not using any add-ons that change the normal behavior of EE, if you navigated to http://example.com/blog/index/ then the main template would be the "index" template of the "blog" template group.

Note: this plugin is available for EE1 and EE2. For the purposes of brevity in the example descriptions we will use the EE2 "channel" instead of the EE1 "weblog", but it works the same for either.

ExpressionEngine Page Titles & Meta Descriptions

So you want nicely formed page titles and relevant meta descriptions. As an example we'll assume this is for your blog on the full-article comment pages. You have a custom field in your blog channel called {blog_page_title} that you want to override the {title} in the page title if the user has filled it in. You want the structure to look like "{blog_page_title} or {title} | {weblog}" and you want your company name appended to all page titles site-wide. You've also got a {blog_meta_desc} custom field that you want to use as the meta description for your blog posts, but you don't want it to print anything if the meta description was left blank.

main template:

	{embed="includes/head"}
	{exp:channel:entries channel="blog"}
		{exp:string:set name="title" default="{title}"}
			{blog_page_title}
		{/exp:string:set}
		{exp:string:append name="title" separator="|"}
			{weblog}
		{/exp:string:append}
		{exp:string:set name="meta_desc"}
			{blog_meta_desc}
		{/exp:string:set}
	{/exp:channel:entries}

includes/head template:

	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html lang="en">
	<head>
		{exp:string:append name="title" separator="|" text="Company Name"}
		<title>{exp:string name="title"}</title>
		{exp:string:output name="meta_desc"}
			{if string != ""}
				<meta name="description" content="{string}" />
			{/if}
		{/exp:string:output}
	</head>

ExpressionEngine Canonical Links

The canonical link tag is a fairly recent addition to our SEO toolboxes. It is used to help the search engines decide which page should be indexed when it can reach the same content via multiple URLs (like http://example.com/ and http://example.com/index.htm both pointing to the same content). Because ExpressionEngine lets you omit the default template group name and the name of the default template within each group, it opens itself up to canonicalization issues. ExpressionEngine also tends to ignore things in the URL beyond the third segment (when it is looking for template_group/template/entry_url_title), meaning that without canonicalization (or 404 pages) there are an infinite number of acceptable URL variants. Even www vs. non-www and URLs ending with a slash versus URLs not ending with a slash can dilute your search engine rankings. Remember it's not necessarily your own site links that matter, but the URLs in links on other sites... Google follows those too.

You can use the string plugin to build the correct link and use the embedded header template to print the canonical link tag in the <head> so the search engine spiders can find it. In this example you can see how to make it work on a basic entry detail page (/template_group/template/entry_url_title/)

main template (blog/details):

	{exp:channel:entries channel="blog"}
		{exp:string:set name="canonical_uri"}
			/blog/details/{url_title}/
		{/exp:string:set}
	{/exp:channel:entries}

includes/head template

	<head>
		{exp:string:output name="canonical_uri"}
			{if string != ""}
				<link rel="canonical" href="http://www.example.com{string}" />
			{/if}
		{/exp:string:output}
	</head>

Accessing Parent Data Within (Reverse) Related Entries

You can use the string plugin to access custom fields from a parent entry within the {related_entries} and {reverse_related_entries} tags.

example template code

	{exp:weblog:entries weblog="default_site" dynamic="off"}
		<h1>{title}</h1>
		{exp:string:set name="parent_title" random}{title}{/exp:string:set}
		<ul>
		{reverse_related_entries}
			<li>{title} - {exp:string name="parent_title" random}</li>
		{/reverse_related_entries}
		</ul>
	{/exp:weblog:entries}

information

Price: $9.95 per site - buy now at devot-ee.com

Compatibility:

  • ExpressionEngine 1.6
  • ExpressionEngine 2.x

Most Recent Version:

  • EE 1.6: String Plugin 1.2.0
    July 28, 2010
  • EE 2.x: String Plugin 1.2.0
    July 28, 2010

documentation