Forum

Thread tagged as: Problem, Field-Types

Slugs not being added to links

Hi there, I'm sure I've missed something obvious here, but I'd appreciate someone pointing it out to me. I'm adding slugs to a template, to make a list/detail setup following this tutorial https://docs.grabaperch.com/perch/content/functions/how-do-i-create-list-detail-pages/ The slug value is not being added to the links.

Template:

<perch:content id="slug" for="heading" type="slug" suppress="true" />
<h3><a href="speaker.php?s=<perch:content id="slug" type="slug" />"><perch:content id="heading" type="smarttext" label="Speaker's name" required="true" /></a></h3>

On the page, each speaker's name is now a link with a href value of "speaker.php?s=". The actual slug content doesn't seem to be showing up. I've already tried republishing all pages, in case it wasn't picking up my edit to the template. What's my next step?

Diagnostic:

    Perch: 3.0.10, PHP: 5.6.31, MySQL: 5.5.32, with PDO
    Server OS: Linux, cgi-fcgi
    Installed apps: content (3.0.10), assets (3.0.10), categories (3.0.10), perch_forms (1.9.1)
    App runtimes: <?php $apps_list = [ 'perch_forms', ];
    PERCH_LOGINPATH: /2017/perch
    PERCH_PATH: /home/ozewai59/public_html/2017/perch
    PERCH_CORE: /home/ozewai59/public_html/2017/perch/core
    PERCH_RESFILEPATH: /home/ozewai59/public_html/2017/perch/resources
    Image manipulation: GD
    PHP limits: Max upload 128M, Max POST 128M, Memory: 768M, Total max file upload: 128M
    F1: 3b606135b33e6a102526838f4152a807
    Resource folder writeable: Yes
    HTTP_HOST: ozewai.org
    DOCUMENT_ROOT: /home/ozewai59/public_html
    REQUEST_URI: /2017/perch/core/settings/diagnostics/
    SCRIPT_NAME: /2017/perch/core/settings/diagnostics/index.php
Julie Grundy

Julie Grundy 0 points

  • 3 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Your tags looks fine to me. Try adding <perch:showall /> to see what's available in your template.

Similar to Hussein says above, is your tag outputting to the screen if you remove the suppress attribute? I wonder if the suppress tag in the first of the slug tags (although the same id) is causing the issue here.

Yes, agree with Jonathan. I have a similar arrangement that works fine. The only difference is I don't have suppress = true on the first line. Try removing that.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

I have tested the exact template and it works fine. The suppress attribute on the first tag shouldn't prevent the second tag from being output.

Is this the same template you are using for your edit form, Julie? If not, this could be the issue. I think the slug field needs to be included in the edit form template, not just the template you use to output to your page (in case they are not the same).

Anyway, if all you want is have a URL-safe version of the speaker's name (e.g. John Silver to be john-silver), you could also use the urlify attribute:

<h3><a href="speaker.php?s=<perch:content id="heading" type="smarttext" label="Speaker's name" required="true" urlify="true" />"><perch:content id="heading" type="smarttext" /></a></h3>

Thankyou! The speaker block uses the same template for the edit and the display. Removing the suppress attribute doesn't seem to have any effect. I switched it to the urlify and now I'm getting lovely URLs on the list of speakers.

If I could trouble you all for a bit more input - changing to the urlify method means that the snippet I copied from the tutorial isn't quite working. What I've got is:

<?php
if (perch_get('s')) {
     perch_content_custom('Speakers', array(
       'template' => 'speaker_block.html',
       'filter' => 'slug',
       'match' => 'eq',
       'value' => perch_get('s'),
       'count' => 1,
    ));
  }
?>

So for a url of speaker.php?s=john-silver, it's giving me the content I set up to show when there are no results (i.e. more details coming soon). I'm assuming the filter is what needs to change, so I put it to 'heading' instead of 'slug'. But of course that is looking for the actual heading text, not the urlified version. Is there a way to format it to look for the urlify text?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Oh I'd forgot what you were trying to achieve in the first place. Using the slug field is a neater way in this case, but you still can get away with using the urlify attribute by filtering on the heading instead of the slug.

perch_get('s') gets you the value of the s parameter in your URL, so you still get john-silver. To filter on the heading you need to replace the hyphens with spaces.

if (perch_get('s')) {
perch_content_custom('Speakers', array(
    'template' => 'speaker_block.html',
    'filter' => 'heading',
    'match' => 'eq',
    'value' => str_replace("-"," ",perch_get('s')),
    'count' => 1,
    ));
}
}