Forum

Thread tagged as: Question, Problem, Configuration

Using perch:content in multiple places

Hey,

So for instance I have <perch:content id="facebooklink" /> in my footer, which is fine and works, but I also want the same information inside a different HTML template?

When I copy the code to another template it just spits out blank?

Thanks

Kieran

Kieran Hunter

Kieran Hunter 0 points

  • 6 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

You would need to show us your template.

I cant untill tonight, so for now ill elaborate encase you can see what I mean.

I have a html template called 'social_media_icons.html' in the template folder, this template is then being called in the footer, so the icons end up in the footer. Which is fine, perfect.

I have another template called seo.html where I want to display some of the same information that is in the 'social_media_icons.html' template into the 'seo' template. So I don't have to write out the same information twice in the admin.

The <perch:content id="facebooklink" /> is in the 'social_media_icons.html' template, and I want the same info it spits out, in the 'seo.html' template, but it just comes up blank.

Hopefully this is a little better to understand? I will still post the templates later on if needs be.

Thank you

Rachel Andrew

Rachel Andrew 394 points
Perch Support

Post your template when you are able, otherwise I'm just guessing.

Just passed by; Kieran, you may be looking for template includes https://docs.grabaperch.com/docs/templates/includes/

Thanks for replying, it's not the whole template I want to include, its just a few of the <perch:content /> tags.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can include template partials. Remember that tags are namespaced - you can't use a perch:content tag in a non-Content app context. For example, if you're talking about the example seo.html template for page attributes, that needs perch:pages tags.

With template includes you can use perch:content tags and then use rescope="parent" to bring them into scope.

Also, apart from the namespacing issue, you can make a template called facebooklink.html and include that in the social media template as well as in another place. So just "modularize" more.

Template partials? importing a partial section from a template?

Is that what I need to do or do I just need to add the re-scope and it will work?

I cant be making template.html files for every line of information id have hundreds.

The facebook link is already being outputted at the bottom of the page so I should be able to output the same information somewhere else just using code.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is your goal content reuse or template reuse?

my goal is to reuse content that is inside a template.

it's just a <perch:content /> tag that I want to use in different templates.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok. Content isn't inside a template. You use a template to display content from a region or a page or an app. You can use multiple different templates to display the same content. You can also pass content into templates from multiple sources.

If you had a region like this in your footer, containing links to your social media profiles:

perch_content('Social links');

that would obviously be using a default template. If you wanted to show maybe just the Facebook link somewhere else, you'd create a new template with just that part in it, and then do this:

perch_content_custom('Social links', [
    'template' => 'social_facebook.html',
]);

Sorry for the confusion from my part :-) Cheers

Yeah that's what i'm doing as of the top part of your response.

As for the bottom part, you are saying just create another template even if it's just for one line of information? (in this case a facebook url).

How would I then display it in a line of code such as:

<meta property="article:publisher" content="FACEBOOKLINK" />

inside 'seo.html' template?

The thing is I already have this link set in another template, I then need it to show in another template (as the above post) aswell.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can pass it into your page attribute template. See here:

https://docs.grabaperch.com/docs/templates/passing-variables-into-templates/

Ok so that's confused me a little.

Where would i put the code for this so that I could use it accross all templates?

and where would the input fields in the admin turn up?

Ill try this out and if to no avail will post my exact codes up. Cheers

Ok, what I need is the google content from 'social_media.html'

/templates/content/social_media.html

<li>
<!-- facebook -->
<a href="<perch:content id="facebook" type="text" label="Facebook" title="true" />" class="facebook" title="Facebook" rel="me">
</a>
<!-- twitter -->
<a href="<perch:content id="twitter" type="text" label="Twitter" title="true" />" class="twitter" title="Twitter" rel="me">
</a>
<!-- YouTube -->
<a href="<perch:content id="youtube" type="text" label="YouTube" title="true" />" class="youtube" title="YouTube" rel="me">
</a>
<!-- Tumblr -->
<a href="<perch:content id="tubmlr" type="text" label="Tubmlr" title="true" />" class="tumblr" title="Tubmlr" rel="me">
</a>
<!-- google plus -->
<a href="<perch:content id="google" type="text" label="Google+" title="true" />" class="googleplus" title="Google" rel="me">
</a>
<!-- CodePen -->
<a href="<perch:content id="codepen" type="text" label="CodePen" title="true" />" class="codepen" title="CodePen" rel="me">
</a>
<!-- Email -->
<a href="mailto:<perch:content id="mail" type="text" label="Email" title="true" />" class="mail" title="Email me!" rel="me">
</a>
<!-- Twitter Handle -->
<perch:content id="twitterhandle" type="text" label="Twitter Handle" suppress="true"/>

</li>

To appear inside the google link in 'facebook.html'

templates/pages/attributes/facebook.html

<!-- Facebook Open Graph -->
<meta property="og:site_name" content="<perch:pages id="sitename" type="hidden" />" />
<meta property="og:url" content="<perch:pages id="url" type="hidden" />" />
<meta property="og:title" content="<perch:pages id="og_title" label="Social title" type="text" escape="true" help="Title for this document with no branding or site name" divider-before="Open Graph Tags" />" />
<meta property="og:description" content="<perch:pages id="description" label="Social description" type="textarea" size="s" escape="true" />" />
<perch:if exists="og_image"><meta property="og:image" content="<perch:pages id="domain" type="hidden" /><perch:pages id="og_image" label="Image when shared" help="Should be at least 1200x630" type="image" width="1200" />" /><perch:else />
<meta property="og:image" content="<perch:pages id="domain" type="hidden" /><perch:pages id="sharing_image" type="hidden" />" /></perch:if>
<link rel="canonical" href="<perch:pages id="url" type="hidden" />" />
<link rel="publisher" href="GOOGLE LINK FROM SOCIAL_MEDIA.HTML HERE"/>
<meta property="og:locale" content="en_GB" />
<perch:if exists="og_type"><meta property="og:type" content="<perch:pages id="og_type" label="Facebook type" type="select" options="article,book,profile,website,video,music" allowempty="true" />" />
</perch:if><perch:if exists="og_author"><meta property="article:author" content="<perch:pages id="og_author" type="hidden" />" /></perch:if>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Your templates don't store content. Which region uses social_media.html ?

My footer.php which is stored in the global folder.