Create the Custom Template for the Page Title Block

We’re going to create the last custom component of our Project page type. The Project custom template for the Page Title block is responsible for this

It’s basically the functionality of the Page Title block, with the value of that Project page’s project_type attribute listed above.

Create the Custom Template

A block type’s custom template can be created in packages/dreamrs/blocks/ or in packages/dreamrs/themes/dreamrs_theme/blocks, if you want the template to be a part of a package. For this particular example, let’s create the custom template within the theme’s directory:

cd public/packages/dreamrs/themes/theme_dreamrs  
mkdir blocks/  
mkdir blocks/page_title  
mkdir blocks/page_title/templates  
touch blocks/page_title/templates/project.php

Then, let’s copy the markup from the theme into this empty file:

Now let’s add the Page Title block to the page below the image.

This block is using the built-in view template, not our custom block view template. So let’s edit the block and assign that custom view template.

Our “Project” custom template shows up in the dropdown without even having to add it anywhere. When we apply this custom template, things look better:

Of course, this is just hard-coded information; we need to modify this template so that it shows the proper title, and the value of the project_type custom attribute. The best way to do this is to copy some of the logic from the original concrete/blocks/page_title/view.php template into our custom template.

This template is much more complicated than we need to worry about. Everything within the $useFilterTitle conditional can be ignored -- this is functionality we’re not going to be using within our site. Basically, we care about the $title variable, which is injected into the template from the controller. So let’s swap out our title with the $title variable.

After this change, the template looks like this in the page.

Great, we have the proper page title showing up now. Now we just need to get the attribute value for the project_type page and print it out on the page. First, let’s set a valid attribute on this page. This can be done from the Page Attributes panel:

Now that we’ve set the Project Type to “Featured Project”, let’s get the attribute displaying within our custom template.

To get the attribute on the current page, just retrieve the current page liked we did before using Concrete\Core\Page\Page::getCurrentPage(), and run getAttribute() on that page, with the handle of the relevant attribute key. If the attribute has been set, this should return an object of the Concrete\Core\Entity\Attribute\Value\Value\Value -- specifically Concrete\Core\Entity\Attribute\Value\Value\SelectValue in this case. This object gives you lots of access into the data that was saved against the page -- but for our purposes all we need to do is output that object with <?=$type?>. All core attribute objects can easily be output to strings with a __toString() method.

With this code in place, our custom template is done!

Loading Conversation