Skip to content

Instantly share code, notes, and snippets.

@adamjohnson
Created October 3, 2012 19:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamjohnson/3829380 to your computer and use it in GitHub Desktop.
Save adamjohnson/3829380 to your computer and use it in GitHub Desktop.
Working with Attributes in Concrete5, download this code and paste into a .html document
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Working with Attributes in Concrete5</title>
<style>
pre { border: 1px solid black; padding:10px; }
</style>
</head>
<body>
<h1>Working with Attributes in Concrete5 CMS</h1>
<h2>Overview</h2>
<p>Attributes allow you to store bits of data with a page. This is useful for when you have pages which really contain bits of data, that we present to the user. Examples for a floor plan: Square Footage, Price, Floor Plan Image, etc. Originally compiled by <a href="http://twitter.com/raverix">Tim Dix (@raverix)</a>. More snippets can be found at <a href="http://www.weblicating.com/doku/doku.php?id=cheatsheet">the Weblicating Cheat Sheet</a>.</p>
<p>To get the value of an attribute, you must first get a reference to the page you're working with. If you're working in one of the theme files, the&nbsp;variable is automatically set for you as:</p>
<pre>$c</pre>
<p>If you don't have a reference to the page variable, you must get a reference like so:</p>
<pre>$c = Page::getCurrentPage();</pre>
<p>With either method, once you've gotten your reference you will be able to use it the same way.</p>
<p>&nbsp;</p><h2>Text</h2>
<p>If the page you're working on has a text attribute with the handle "some_attribute", then you could display this to the user with the following:</p>
<pre>&lt;?php echo $c-&gt;getAttribute('some_attribute') ?&gt;</pre><h2>Text Area</h2>
<p>Similar to the text attribute, if the page you're working on has a text area attribute with the handle "some_attribute", then you could display this to the user with the following:</p>
<pre>&lt;?php echo $c-&gt;getAttribute('some_attribute') ?&gt;</pre><h2>Checkbox</h2>
<p>The checkbox attribute is useful if you want to store true/false, on/off type of information. For example; floor plan sold out, featured on special, has garage? To display information to the user using a checkbox attribute, we test the value of the checkbox, and display information based on it. Let's use the example of whether or not the unit has a garage. Let's name the attribute "floorplan_has_garage."</p>
<pre>Garage: &lt;?php echo $c-&gt;getAttribute('floorplan_has_garage') ? "Yes" : "No" ?&gt; </pre>
<p>If the checkbox is checked, anything within the first first set of double quotes (") will be displayed, otherwise, everything within the second set will be displayed.&nbsp;This will display "Garage: Yes" if the checkbox is selected, or "Garage: No" if not.&nbsp;</p>
<p>If you had something like Sold Out, where you only wanted to display to the user if it's true, you can place everything in the first set of quotes, and leave the second one empty, like so:</p>
<pre>&lt;?php echo $c-&gt;getAttribute('floorplan_sold_out') ? "&lt;h2&gt;Sold Out&lt;/h2&gt;" : "" ?&gt;</pre><h2>Date/Time</h2>
<p>Coming Soon</p><h2>Image/File</h2>
<p>When you get a Image/File attribute, you get a reference to a file object. You can then do anything you need with that file object, depending on it's type. Because you're working with a complex data type, and not just a simple value, the first step in all of these is to ensure that you have a proper file, so we use the following:</p>
<pre>&lt;?php<br>if($file = $c-&gt;getAttribute('some_file_attribute') )<br>{<br><span style="white-space: pre;"> </span>//Any thing we want to do on the image in here<br>} <br>?&gt;&nbsp;</pre>
<p>Display Image</p>
<pre>&lt;?php<br>if($file = $c-&gt;getAttribute('some_image_attribute') )<br>{<br><span style="white-space: pre;"> </span>$ih = Loader::helper('image');<br><span> </span>$ih-&gt;output($file, $file-&gt;getTitle());<br>}<br>?&gt;&nbsp;</pre>
<p>Image Thumbnail, 100 max width, by 200 max height:</p>
<pre>&lt;?php<br>if($file = $c-&gt;getAttribute('some_image_attribute') )<br>{<br><span style="white-space: pre;"> </span>$ih = Loader::helper('image');<br><span> </span>$ih-&gt;outputThumbnail($file, 100, 200, $file-&gt;getTitle());<br>}<br>?&gt;&nbsp;</pre>
<p>Download File</p>
<pre>&lt;?php<br>if($file = $c-&gt;getAttribute('some_downloadable_attribute') )<br>{<br><span> </span>$url = $file-&gt;getVersion()-&gt;getDownloadURL();<br><span style="white-space: pre;"> echo "&lt;a href='$url'&gt;Download File&lt;/a&gt;";</span><br>}<br>?&gt;&nbsp;</pre>
<p>Email Example (Absolute URL)</p>
<pre>&lt;?php<br>if($file = $c-&gt;getAttribute('some_downloadable_attribute') )<br>{<br> $s = @getimagesize($file-&gt;getPath());<br> print '&lt;img class="ccm-output-image" alt="' . $file-&gt;getTitle() . '" src="' .BASE_URL. $file-&gt;getRelativePath() . '" width="' . $s[0] . '" height="' . $s[1] . '" /&gt;';<br>} <br>?&gt; </pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment