Skip to main content
Topic: Which template file to append Schema markup dynamically (Read 3658 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Which template file to append Schema markup dynamically

Link tree code exists in top and bottom of every page , when I modify the one on top in index.template.php  , the other at bottom catch the same modifications.
I'm adding micro-data tags to the top one only , and need it to appear in page only once , any help about this ?
thank you
 
Last Edit: March 24, 2017, 09:18:58 am by sherif

Re: Separated code for bottom linktree

Reply #1

Depending on the scope of the changes.
If you plan on doing it for your site only or for an addon I have one method in mind, if you plan on pushing it to the core I have another.
Bugs creator.
Features destroyer.
Template killer.

Which template file to append Schema markup dynamically

Reply #2

I changed the name of this topic , because the question is different now :
Thank you for your quick reply , however I had to give up on this , I was making changes into the core and discovered how time consuming and risking creating new bugs without knowing .

I reset everything back to original and made the changes using php code reading the $context['linktree'] then create JSON script dynamically to append BreadcrumbList tag for every page.
The code is in a single php file which required_once before the end of the file index.template.php , and its done.

Answering your question ,This can be used for any ElkArte forum , not only my site , but wasn't able to make it addon although its very simple and short - this is what happens for newbies when not finding your  invisible documentation&examples  ::) 
Code: [Select]
<?php

$NNN = <<<EOT

<script type="application/ld+json">
{
 "@context": "http://schema.org",
 "@type": "BreadcrumbList",
 "itemListElement":
 [
EOT;

echo $NNN;

$icount = count($context['linktree'] );
foreach ($context['linktree'] as $pos => $tree)
{
$name = $tree['name'];
$url= $tree['url'];
$item = "  {
   \"@type\": \"ListItem\",
   \"position\": $pos,
   \"item\":
   {
    \"@id\": \"$url\",
    \"name\": \"$name \"
    }
  }";

  echo $item;
  if($pos < ($icount-1)) echo ",";
  echo "\n";
}

$NNN = <<<EOT
 ]
}
</script>
EOT;

echo $NNN;

$NNN = <<<EOT

<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "http://elkarte.net/community/index.php",
"potentialAction": {
"@type": "SearchAction",
"target": "http://elkarte.net/community/index.php?action=search;sa=results&search={search_keywords}",
"query-input": "required name=search_keywords"
}
}
</script>

EOT;

//json for homepage only , need fix
if($icount==1)
echo $NNN;

?>

I still have 3 more tags to append to the "topic display page" , so, which template file should I work on for that ?
also what is the easiest way to check for homepage presence to execute something only for it ?

Thanks

Re: Which template file to append Schema markup dynamically

Reply #3

You should be safe if you simply create a copy of default theme and do your testing on that copy because the file that you are editing is a template file. Otherwise, keep a backup or restore file ready and handy.

Re: Which template file to append Schema markup dynamically

Reply #4

Just copied the theme , but the copy contains only the index.template.php
What is the right procedure when needing to edit - for example - MessageIndex.template.php

Re: Which template file to append Schema markup dynamically

Reply #5

My idea for an addon was a bit different, but if you can do it like that, then you can:
1) use the hook integrate_current_action
2) depending on the action decide what to show,
3) append the json snippet to $context['html_headers'].

I know this is way too short to give you any hint, but at the moment it's the maximum I can write.
I'll expand over the weekend (or tonight).
Bugs creator.
Features destroyer.
Template killer.

Re: Which template file to append Schema markup dynamically

Reply #6

@sherif, if you can follow what @emanuele said above is better. But if you think it is quite hard for now, just copy the relevant template file(s) you need to test to your custom/test theme folder. There you can do whatever you want without worry of affecting the default theme.

 

Re: Which template file to append Schema markup dynamically

Reply #7

The attached is the general idea of the code.

I'm not sure what should be specific for each page, so I didn't do much on that respect, but $current_action has the information of the page being shown.

In order for the file to work, it should be added to the database with something like:
Code: [Select]
<?php
require_once('SSI.php');
add_integration_function('integrate_current_action', 'Microdata_Integrate::integrate_current_action', 'SOURCEDIR/Microdata.integrate.php');
and put the file in /sources/.

Re: Which template file to append Schema markup dynamically

Reply #8

Thanks @emanuele , downloaded the file and working with your instructions ,
will post back what I could achieve 

Re: Which template file to append Schema markup dynamically

Reply #9

I need to get some informations while on topic display page : index.php?topic=....
the following are required for the first post, then for every single reply:
    url
    dateCreated
    text  
    poster info
any hint about the template file that displays topic (if any) , and the variables that contain the mentioned info would be very helpful
thank you

Re: Which template file to append Schema markup dynamically

Reply #10

Display.template.php
Bugs creator.
Features destroyer.
Template killer.

Re: Which template file to append Schema markup dynamically

Reply #11

Hi guys, new member here with a specialty in semantic-web programming. I just installed an instance of Elkarte and have been editing the templates the last few days. @sherif, i favor RDFa to JSON-ld in most situations, namely because of the DRY Violation.

IMO, the best way to edit the templates is to...

  • open all the files in your editor,
  • open the page you're working on in your browser,
  • inspect the element in question in your Dev Tools,
  • find a unique part of markup you can search all files for,
  • add the semantics to the markup
  • copy the variable (which produces the value in your semantics) and search all files for it to make sure your changes are applied everywhere.
  • Validate your semantics

I'm not quite ready to share a link to my project but as for my progress so far:
 i've fully described most of the "index", some of the "board" and "topic" templates. I've fully described SearchAction (potential, active, completed and failed states) & part of FindAction on the "search results" page, I'm currently working on pagination links in search results.

About choosing the right schema.org Classes, my tree looks like this:

  • WebPage (or ProfilePage or SearchResultsPage) --on <body>, contains: BreadcrumbList, SearchAction, UseAction --for the "Online Now" info.)
    • DigialDocument (describes the forum)
      • DigialDocument (describes boards)
        • DiscussionForumPosting (describes topics)
          • Comment (describes replies)

I do have a question, however. @emanuele I can access...
Code: [Select]
$topic['first_post']['icon_url']
...on the MessageIndex.template, I'm also trying to access this on the GenericBoards.template, I've tried may variations of...
Code: [Select]
$board['last_post']['icon_url']
...to no avail. Any suggestions?
Last Edit: May 16, 2017, 02:14:25 am by Atlas

SPLIT: How to deal with first/last_post in GenericBoards.template

Reply #12

One or more of the messages of this topic have been moved to Support - http://www.elkarte.net/community/index.php?topic=4465.0
Bugs creator.
Features destroyer.
Template killer.