4

I have an entry type (outsideCourse) that will never need its own page. It only exists so I can manipulate it in various lists of required courses. Thanks to Mats Mikkel Rummelhoff, I have a macro to plug its field attributes into a search URL for another website.

Another entry type in the same section, safsCourse, DOES have its own template URL scheme.

I don't want to have a mostly blank page show up if someone happens to go to the entry page for one of the outsideCourse entries (example: localhost/craftcms/courses/ecological-economics).

The options I can think of:

  1. Somehow eliminate slugs or URLs for the outsideCourse entry type, so their URLs go to the 404 page
  2. .htaccess redirects
  3. A separate template just for this entry type that redirects either to my preferred destination (the http://msu.edu link generated by my macro) or the index page for this section (localhost/craftcms/courses).
  4. One template for both entry types, with a conditional redirect.

I've been working on Option 4. I've tried adding the following to the very beginning of my courses/_entry.html template, before {% extends '_layout' %}:

{% if craft.entries.section('courses').type('outsideCourse') %}
     {% import 'partials/_helpers' as helpers %}
        {% redirect helpers.msuLink(entry) %}
 {% else %} 

It tries to go to http://localhost/craftcms/%20%20%20%20https://reg.msu.edu/Courses/Request.aspx?SubjectCode=EEP&CourseNumber=260#Results and gives me a 403 Forbidden error.

If I replace helpers.msuLink(entry) with "http://msu.edu", it redirects but ignores the conditional.

So here's what I'm wondering:

  • Why isn't the conditional working?
  • How can I add an absolute link to a redirect without just writing a string?
  • Should I be going about this in a different way altogether?
4

The reason that you get that odd URL with the {% redirect %} tag is just because there's some whitespace at the beginning of the value returned from your helpers.msuLink() macro. Using the |trim filter (which removes whitespace from both ends of a string) fixes that:

{% redirect helpers.msuLink(entry)|trim %}

If you prefer, you could also modify your macro so it returns the value without whitespace, by using the {% spaceless %} tag – then you don't need the |trim filter:

{% macro msuLink(entry) %}
{% spaceless %}
    {{ 'https://reg.msu.edu/Courses/Request.aspx?SubjectCode=' ~ entry.deptCode ~ '&CourseNumber=' ~ entry.courseNumber ~ '#Results' }}
{% endspaceless %}
{% endmacro %}

As for the conditional, I don't believe you want the craft.entries query there – just put the following at the top of your courses/_entry.html template (note that I'm also putting the {% import %} statement outside the conditional – it doesn't really matter but its cleaner and shouldn't impact performance):

{% import 'partials/_helpers' as helpers %}

{% if entry.type == 'outsideCourse' %}
    {% redirect helpers.msuLink(entry)|trim %}
{% endif %}

{# Rest of template below #}
...

If you decide that having the outsideCourse URLs 404 instead of redirect makes the most sense (I believe it would, and is cleaner, in most cases), the easiest way to achieve that is to put this at the top of your entry template:

{% if entry.type == 'outsideCourse' %}
    {% exit 404 %}
{% endif %}

{# Rest of the template below #}
...
  • You've come to the rescue again! That makes it work exactly how I want it to. I tested the 404 option as well, and that also works. Thanks. – Katie Fritz Aug 26 '16 at 22:48
  • Awesome, @KatieFritz - I'm happy it worked out :) – Mats Mikkel Rummelhoff Aug 27 '16 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.