0

The goal is to have a variable(link) merged with prefix "any special character", that will not break the URL(this was pointed to my about character in URL lately, but in this question, We use "#"). So the final output would be "site.com/#variable" (in my case it is the author of an entry and a link to author's profile.

Also, I'd have to route it to get the correct template loaded. The whole reason behind is, that the current prefix would avoid of potential conflicts with other templates in case, some username will be 'index'.

Dominik Krulak
  • 1,562
  • 14
  • 27
  • "linkToLeft" is already different from "linkToRight"? – carlcs May 26 '15 at 15:02
  • Understand this as "link" is variable and "ToLeft" and "ToRight" are two different links of type. Both serve different type of content. To you question, Yes it's already different. – Dominik Krulak May 26 '15 at 15:07
  • I'm not sure I'm following "link" is variable and "ToLeft" and "ToRight" are two different links of type.. Maybe edit the original question with some further clarification? – Brad Bell May 26 '15 at 15:49
  • Hope, the question's update will help. – Dominik Krulak May 26 '15 at 16:40
  • Where does the "toLeft" and "toRight" go in the final link? And I still don't get it, why do you want to prefix it with a "#"? They are already different! – carlcs May 26 '15 at 17:20
  • It doesn't matter where it goes. What matters is, what template gets loaded. The reason Why I defined the links with "ToLeft" and "ToRight" is, that both represent different types of content, which require two different templates. If I would want to set the route in CP for both links, It would looks like * loads "_ToLeft" and * loads "_ToRight" template. This would not work. So I think, the special character would actually help here. – Dominik Krulak May 26 '15 at 17:35
  • The hashtag denotes a URL fragment and will essentially break your URL at the insertion point - as such, it's probably a poor choice of special character. More to the point, how are the two types of content different? Are they different entry types or the like? A decent way to solve this would be to point both URLs to the same, generic template, where you include the specific template based on the content - eg {% include entry.type ~ '-partial' %} – Mats Mikkel Rummelhoff May 27 '15 at 11:52
  • Authors and entries. These are the content types. Doing it this way, I wouldn't achieve a special character in the link. The question is unclear and I don't know How more precisely to clarify it for others, so I've proposed to close it. – Dominik Krulak May 27 '15 at 13:18
  • Is the idea that if you had two urls like #author1linkToRight and #author1linkToLeft you would be able to separate the #author1 and linkTo... parts based on whether it has the special character prefix? So then you'd have two variables, #author1 and linkTo... that you'd be able to use to pull the correct template in? – Alec Ritson May 27 '15 at 15:05
  • Kind of not @Alec Ritson. I updated the question and it may link a little bit to this question – Dominik Krulak Jun 02 '15 at 20:08
  • 2
    If this gets reopened i'm 99% sure I have a solution... – Alec Ritson Jun 04 '15 at 13:58
  • @AlecRitson Go for it :) – Lindsey D Jun 05 '15 at 06:24

1 Answers1

4

So from reading your updated answer and looking at the other question you mentioned I would assume you are trying to create prefixed dynamic links that don't conflict with your routing.

For example site.com/about would link to your about page and site.com/#about would load up your custom template.

Problem

The problem with using a # for your trigger is that really this should be used for linking to internal sections on a page and with the above site.com/#about would actually just link to your index page and if you had a <div> with an id of about you'd find the scroll position at that div, so I think using a # is going to give you a lot of headaches.

Solution

Perhaps use the @ sign? if it's good enough for twitter to use then I don't think you'll have many problems using it.

So, in your config/routes.php you would have:

return array(
 '(?P<username>\@\w+)' => '_someCustomTemplate',
);

This will match anything starting with an @, assign it to username a variable in your template and load it up, I tested on the Happy Lager install and it worked like a charm, /about goes to the about page /@about goes to my custom template

So in your custom template you could have the following:

{# This will get rid of the @ #}
{% set username = username|replace({'@': ''}) %}

{% set user = craft.users.username(username) %}

If you want to change the trigger to something else, just change @ in the route expression and then in the replace filter, but that should do the trick for you and avoid conflicts!

on a side note, I replaced @ with a # and it didn't work, just went to the home page

Alec Ritson
  • 4,529
  • 1
  • 19
  • 34