Urlify but with underscores instead of dashes

  • This is from an old post Simon post a couple years ago. I don't see an answer though. I'm currently using 'urlify' to generate a filter. There are two names that have an &, so the 'urlify' changes it to a dash. But I think the perch_blog_custom has an issue displaying a 'value' with a dash in it.


    So - is there a way to urlify but with underscores instead of dashes? Code snippet please?

  • Sure thing. So the post.html gives the users an options to choose an "Industry" from a dropdown menu. This dropdown provides the title of the industry in the proper format. For example: Insurance (capitalized)


    But I'm using the same dropdown to set the class of the DIV, so I'm using URLify to set that as well. So it's format is then class="insurance".


    This works just as I expect here:

    PHP
    1. <?php perch_blog_custom([
    2. 'sort' => 'postDateTime',
    3. 'template' => 'blog/post_in_column.html',
    4. 'sort-order' => 'DESC',
    5. 'filter' => 'industry',
    6. 'match'=>'eq',
    7. 'value'=>'insurance',
    8. ]); ?>

    But 2 options in drop down menu have an ampersand. For example: Energy & Utilities. But the URLify'd version doesn't seem to want to filter. I assume this is because of the dash.

    PHP
    1. <?php perch_blog_custom([
    2. 'sort' => 'postDateTime',
    3. 'template' => 'blog/post_in_column.html',
    4. 'sort-order' => 'DESC',
    5. 'filter' => 'industry',
    6. 'match'=>'eq',
    7. 'value'=>'energy-utilities',
    8. // with the dash, this filter doesn't work. So can URLify use an underscore instead?
    9. ]); ?>
  • The urlify attribute in a template only affects the output. It does not affect how the data is saved. So you should be able to use Energy & Utilities:


    PHP
    1. perch_blog_custom([
    2. 'sort' => 'postDateTime',
    3. 'template' => 'blog/post_in_column.html',
    4. 'sort-order' => 'DESC',
    5. 'filter' => 'industry',
    6. 'match'=> 'eq',
    7. 'value'=> 'Energy & Utilities',
    8. ]);


    This has nothing to do with dashes. Using dashes for field IDs is not advised (https://docs.grabaperch.com/templates/attributes/id/), but that's a completely different thing.