Forum

Thread tagged as: Question

Using content:id in pages attribute

Hi,

I made a post some time ago:

https://forum.grabaperch.com/forum/07-16-2017-using-content-from-a-perchcontent-id-with-perchsystemset-var

which is now closed, however I would like to make my intention more clear.

I have a Perch content template:

<template id="upper_template">
<style><perch:template path="upper_image_styles.css"/></style>
<picture>
<source srcset="<perch:content id="webp" type="text" label="WebP image"/>" type="image/webp">
<img src="<perch:content id="jpeg" type="text" label="JPEG image"/>" alt="<perch:content type="text" id="alt" label="Alternate Text" help="Define alternate text for JPEG image"/>" class="<perch:content id="imageclass" type="select" label="Image Class" options="Home|image_home,Custom|image_custom,Custom 1|image_custom1,Custom 2|image_custom2,About|image_about" help="Select Image Classname" allowempty="true"/>">
</picture>
</template>

<article></article>

<script><perch:template path="upper_script.js"/></script>

as well as a Perch attribute template:

<meta property="fb:app_id" content="<perch:pages id="fbappid" type="hidden"/>">
<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="businessname" type="hidden"/>">
<meta property="og:description" content="<perch:pages id="og_description" label="Social description" type="textarea" escape="true"/>">
<meta property="og:image" content="<perch:content id="jpeg" type="hidden"/>">

I have managed to obtain data for all fields bar og:image so I'm wondering how, what I am trying to do may be possible.

The response to my previous post was I believe related to this:

https://docs.grabaperch.com/perch/content/functions/content-from-multiple-pages-and-regions/

but I am not sure if that is relevant.

I can obtain content from the first four og tags is because they are using Perch variables but regarding the last og tag, I am wondering if PHP would need to be involved in some way.

Kind regards

Garth Holmes

Garth Holmes 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you see your value if you use <perch:showall /> ?

Your tag needs to be perch:pages not perch:content.

Hi Drew,

So the perch:pages and perch:content namespaces are not interchangeable or compatible in any way or there is no way to use data from one namespace in another?

Cheers

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

If you set a variable with PerchSystem::set_var(), you should be able to use it in any namespace.

// $image_url = get your image 
PerchSystem::set_var('jpeg', $image_url);

perch_page_attributes([
      'template' => 'your_template.html'
]);

In your attributes template:

<meta property="og:image" content="<perch:pages id="jpeg" type="hidden"/>">

Hi Hussein,

Following is what I have first on every page:

<?php (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/MSIE (.*?);/',$_SERVER['HTTP_USER_AGENT'],$matches));
if(count($matches)<2){preg_match('/Trident\/\d{1,2}.\d{1,2}; rv:([0-9]*)/',$_SERVER['HTTP_USER_AGENT'],$matches);}
$userAgent=isset($_SERVER['HTTP_USER_AGENT'])
?strtolower($_SERVER['HTTP_USER_AGENT'])
:'';
if(count($matches)>1){
$version=$matches[1];
switch(true){
case ($version<=11):
Header("Location:/unsupported/");
break;
}
}
;?>
<?php include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php');?>
<?php set_include_path($_SERVER['DOCUMENT_ROOT']);?>
<?php header('Last-modified: . the_modified_date()');?>
<?php perch_layout('doctype.layout');?>
<?php
$url="https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
PerchSystem::set_vars(array(
'businessname'=>'',
'googleid'=>'',
'fbappid'=>'',
'sitename'=>'',
'url'=>$url,
'heading3'=>perch_pages_title(true),
'image',$image
));
?>
<title><?php echo(PerchSystem::get_var('businessname'));?> — <?php perch_page_attribute('descriptor'); ?></title>
<meta charset="utf-8">
<?php perch_page_attributes(array('template'=>'default.html'));?>

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','','ga');
ga('create','','auto');
ga('send','pageview');
</script>

My default.html page:

<perch:pages id="descriptor" label="Title Descriptor" suppress="true"/>
<link rel="stylesheet" href="/_templates/content/_site.css"/>
<meta name="viewport" content="width=device-width">
<link rel="manifest" href="/_json/manifest.json">
<meta name="msapplication-config" content="/_xml/browserconfig.xml">
<perch:template path="pages/attributes/seo.html"/>

and SEO page:

<meta property="fb:app_id" content="<perch:pages id="fbappid" type="hidden"/>">
<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="businessname" type="hidden"/>">
<meta property="og:description" content="<perch:pages id="og_description" label="Social description" type="textarea" escape="true"/>">
<meta property="og:image" content="<perch:content id="jpeg" type="hidden"/>">
<perch:if exists="og_type"><meta property="og:type" content="<perch:pages id="og_type" label="Facebook type" type="select" options="article,website" allowempty="true"/>"></perch:if>
<perch:if exists="og_author"><meta property="article:author" content="<perch:pages id="og_author" type="hidden"/>"></perch:if>

The variable that I am wanting to set, will come from this .html template is is hoped:

<picture>
<source srcset="<perch:content id="webp" type="text" label="WebP image"/>" type="image/webp">
<img src="<perch:content id="jpeg" type="text" label="JPEG image"/>" alt="<perch:content type="text" id="alt" label="Alternate Text" help="Define alternate text for JPEG image"/>" class="<perch:content id="imageclass" type="select" options="Home|image_home,Custom|image_custom,Custom 1|image_custom1,Custom 2|image_custom2,About|image_about" allowempty="true" label="Image Class" help="Select Article Image Classname"/>">
</picture>

but that being a .html template, of course comes later so would this be a reason why this might not work?

Thank you for any replies.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

but that being a .html template, of course comes later so would this be a reason why this might not work?

Yes. You need to get the image before PerchSystem::set_vars() and before perch_page_attributes(). In other words:

// your region

// get image from your region

// set image as variable

// output page attributes
perch_page_attributes(array('template'=>'default.html'));

// output region wherever you want

By the way, the SEO template you posted still uses perch:content. It should be perch:pages.

Also your tag id should be the variable name you set with PerchSystem::set_var(). So if you use PerchSystem::set_var('image', $image_url), your tag should be:

<meta property="og:image" content="<perch:pages id="image" type="hidden"/>">

Hussein Al Hammad said:

but that being a .html template, of course comes later so would this be a reason why this might not work?

Yes. You need to get the image before PerchSystem::set_vars() and before perch_page_attributes().

So when you say "region" are you referring to the outputted content via a .html template in PHP?

By the way, the SEO template you posted still uses perch:content. It should be perch:pages.

For the variable reference jpeg I have corrected but this seems irrelevant since the order of templates and regions determines what data can and cannot be reused.

Also your tag id should be the variable name you set with PerchSystem::set_var().

I believe I have the correct variable name for <perch:pages id=""/> but this situation is rather disappointing and while I am no developer or programmer I was of the impression that PHP could work with XMLDOM and the latter could be used anywhere in markup.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

So when you say "region" are you referring to the outputted content via a .html template in PHP?

// doesn't output anything
$my_region = perch_content('Intro', true);

.
.
.

// when you are ready to output your region
echo $my_region;

You can refer to the documentation for more details on this.

This is what I have now:

<?php include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php');?>
<?php perch_content_create('upper_content',array('template'=>'_upper_content.html'));?>
<?php perch_page_attributes(array('template'=>'default.html'));?>
<?php PerchSystem::set_vars(array('para'=>perch_page_attribute('navtext')));?>
<head></head>
<body>
<?php perch_content_custom('upper_content');?>
</body>
</html>

in default.html there is a <perch:pages/> field (which it is hoped I can create a variable in PHP).

<perch:pages id="navtext" label="Navigation Text" suppress="true"/>

then my .html template:

<p><perch:content id="para" type="hidden"/></p>

I don't know why this doesn't work, when it is so simple to use text from perch_pages_navigation_text() and perch_pages_title()

If I am doing something wrong then fine, but if it is a limitation of the system, then I guess it is not (so).

Garth, In the last line of code, what's the html template? Do you intend to call that template with perch_content_custom?

If so, you need:

<?php
  perch_content_custom('upper_content', array(
     'template' => 'name-of-template',
  ));
?>

https://docs.grabaperch.com/functions/content/perch-content-custom/

A couple more things... as I just tested it to see if I could get it working...

Here's what I had to do:

I changed this:

<?php PerchSystem::set_vars(array('para'=>perch_page_attribute('navtext')));?>

to

<?php PerchSystem::set_var('para', perch_page_attribute('navtext', array('template' => 'garth_out.html'),true));
?>

I added two things: a template attribute and a true value.

I added your navtext field to an attribute template called garth.html and suppressed it:

<perch:pages id="navtext" type="text" label="Para" required="true" suppress="true" />

The reason I added the template attribute to perch_page_attribute was to get the navtext value through. I added the true value to keep it from printing to the page. The reason I added the suppress=true to the attribute template was to keep it from outputting when you call <?php perch_page_attributes(array('template'=>'default.html'));?>

Then to get the navtext field to be read into perch_page_attribute, I needed another attribute template (garth_out.html) that had:

<perch:pages id="navtext" type="text" label="Para" required="true" />

It seems perch can't get the field value if the field has suppress="true".

With that, the set_var worked and I had the para value available in my content template.

The page template:

<?php include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php');
    perch_content_create('upper_content',array('template'=>'garth/_upper_content.html'));
    perch_page_attributes(array('template'=>'default.html'));
    PerchSystem::set_var('para', perch_page_attribute('navtext', array('template' => 'garth_out.html'),true));
?>
<head></head>
<body>
<?php 
  perch_content_custom('upper_content', array(
     'template'      => 'garth/_upper_content',
  ));
?></body>
</html>