Hot answers tagged

42

The cleanest way I have found to achieve this is to create a generic template for the structure as well as a custom template for each entry that needs one, ensuring that its path matches the entry's uri. I then put this single line of code in the structure's selected template and it works beautifully: {% extends [entry.uri, '_generic'] %} This should ...


10

I would not particularly advise creating a uri pattern that was just 'www.site.com/username' because usernames can essentially be anything which might cause routing conflicts (i.e. if the username is 'index' for example). You could however use something like 'www.site.com/members/username' or 'www.site.com/u/username'. One way to do this would be to add a ...


9

For this example we have two domains: http://www.example.com http://subdomain.example.com To use different templates for the subdomain, we can accomplished this with these steps: 1) Setup subdomain to point to a different directory on your server: Domain: /home/wherever/public_html Sub-domain: /home/wherever/public_html_subdomain 2) Copy your index.php ...


8

You can do this with Craft’s {% redirect %} tag. Just put this in your /index.html template: {% redirect 'news' %} If you want to redirect to the latest entry in your News section, as you mentioned in the comments above, just fetch the entry first: {% set latestNews = craft.entries.section('news').order('postDate desc').first() %} {% redirect latestNews....


8

You can retrieve the entry by grabbing it from the URL as a variable: {% set entrySlug = craft.request.lastSegment %} And then using craft.entries to find the matching content: {% set entry = craft.entries.slug(entrySlug).find() %} Alternatively when you set up your route, so that Craft knows which template to load when someone is requesting your entry, ...


7

There's alot of different things that could be going on here. First, if your action is named actionPayWithPaypal (as in the code above) the action url should be paypalPayments/payment/payWithPaypal I guess? You won't be able to hit the cocktail recipes method directly from your browser since it requires the request to be a post. But you should be able to ...


7

First, I'd recommend that you have a total of 3 templates: _child.html for your child entries, _parent.html for your parent entries, and finally _entry.html which will basically work as a "controller" template (the only code contained in _entry.html should be the code that includes either the child or parent templates). You can, of course, name the templates ...


7

To pull this off, first you’ll have to make sure all of your Element API endpoints begin with something consistent, like api/ or data/, etc. Then create a route in craft/config/routes.php that will match everything besides those Element API endpoints: <?php return array( '(?!api\/).*' => 'index' ); 'index' is the template path the requests ...


6

You might check out the routing docs on accessing subpatterns in your templates. Here is the example given in the docs (which provides the variables 'year' and 'month' to the template that correspond to the matched uri segments — which would match `news/2015/02'): 'news/(?P<year>\d{4})/(?P<month>\d{2})' => 'news/_archive', So I would ...


6

Alan describes how to setup a route via the Settings->Routes area in the CP. If you happen to be managing file based routes you can setup a similar route like so: return array( // Your Custom Section 'username/blog/(?P<slug>.*)' => 'blog/_entry' ); The third segment in your route (?P<slug>.*) creates the variable that will ...


6

This is very, very simple to do... just put every single template into a specific folder, then prefix the folder with an underscore: /craft/templates/_everything/ Template files beginning with underscores are hidden, similarly, template folders beginning with an underscore hide everything inside.


6

In case someone uses Craft 3, this should work: <? return array( '<url:(?!api\/).*>' => ['template' => 'index'] );


5

Your templates will be private if they are contained in a folder prefixed with a "_"... So just make a folder like that, and put everything else inside of it: /craft/templates/_everything/myprivatefile.html


5

Yes it’s possible. First you will need to update your route key so that you’re capturing the name parameter with a valid named subpattern. (You were close, but missing the actual pattern part of it). return array( 'api/test/(?P<name>[^\/]+)' => ['action' => 'api/element/section'], ); Any named subpattern matches in your route will be passed ...


5

Currently, the conditional redirect is the best way. You can use it to load a template dynamically in the structure's generic _entry template using the include tag. My suggestion would be to add a lightswitch field that tells the entry to load a custom template. Then in the _entry template do something like this: {% if entry.customTemplate %} {% ...


5

Figured it out! I went ahead and took at look at how Craft's renderTemplate method works which cleared it up for me. A few things... Craft caches CSS/JS resources inside of getHeadHtml() and getFootHtml() which are automatically injected into every template rendered through renderTemplate. Injecting these into my template fixed the JS problem. I forgot to ...


5

To do this, copy index.php and .htaccess from a.com’s webroot into b.com’s, and make sure the $craftPath variable is correct. Then, around where $craftPath is getting defined, override $_GET['p'] to make Craft think that the URI actually started with subpath: $craftPath = '../craft'; $_GET['p'] = 'subpath/' . (isset($_GET['p']) ? $_GET['p'] : ''); // ... ...


5

Not sure exactly what you trying to do here? Call a Twig template from Angular? Twig is a server side technology, Angular is getting parsed on the client side. The only way Craft is going to parse a Twig template is when it encounters a URL. Instead of calling the Twig template directly, you call the URL, which could match a template. Inside templates, ...


5

Yes, possible. You just need to override the URL routing within the subdirectory, so requests to /sub/* get routed to /sub/index.php rather than /index.php. For example, if you’re using Apache, just place Laravel’s .htaccess file within your subdirectory.


5

You can use the registerSiteRoutes() hook in your plugin's primary class. Assuming your custom URL formats are stored in an array in a plugin setting called customRoutes (use AttributeType::Mixed for arrays), and that the array consists of key/value paired routes and template paths: public function registerSiteRoutes() { return $this->getSettings()['...


5

I'm not sure I understand your question, but to try: There actually isn't a "standard Craft routing", for channel Sections or anything else. In your channel's Section settings (available via Settings -> Sections -> [your event section]), there's a field called Entry URL format. Simply add {slug} to that field (and nothing else), and you should be good to ...


5

There are two ways you can set that up, depending on whether you want Brand entries to have a similar URI format (just without the 3rd segment), e.g. category/brand. Assuming you do, edit your Brands section, and give it the following Entry URI Format: Craft 2: {category.first().slug}/{slug} Craft 3: {category.one().slug}/{slug} Then edit your Offers ...


5

The issue is that your Asset Volume URL is @web/image but your Asset Volume path is @webroot/assets/art That means that Craft will look for the images on disk (to do transforms, etc.) in web/assets/art but it will generate URLs for frontend requests that point to http://example.com/image -- where presumably nothing exists. These should probably be: URL: @...


4

One easy way to work around this is to use a query string instead of a segment. You'd redirect the user to for instance /contact?receipt=thanks instead of /contact/thanks, and you change the conditional in your template to something like this: {% if craft.request. getParam('receipt') == 'thanks' %} ...thank you message {% endif %} I've always felt ...


4

If you want one of your channel entries to be the homepage, simply include the channel entries' template to your home page index.html: {# Set the `entry` variable manually #} {% set entry = craft.entries.section('news').first() %} {# Load the channel entry template #} {% include "news/_entry" %} With this setup, your visitors would actually be on your ...


4

Assuming that those pages are working off of different entry types, you should check out this great help article... How can I give each Entry Type its own template? Basically, you let a parent template decide which child template to load, based on the current entry type.


4

You can either set the URL Format of the landing pages channel to {slug} (the same as your normal pages) Or you could try putting the landing pages in the normal pages structure, but give them a new entry type so you can use different fields.


4

The CP route settings don’t have a token for matching multiple URI segments. However you can pull it off from craft/config/routes.php instead: return array( 'blog/(.+)' => 'path/to/template', ); You’ll be able to access whatever was captured by (.*) from your templates via matches[1].


4

When your plugin has a camel cased name you need to make sure you camel case your references to it, such as when you try and visit an action url. I assume this is due to when craft tries to resolve the url it sees pluginname and pluginName as two different things, much like php variables. I would also assume this is due to the way Craft autoloads classes. ...


4

Remove the / at the end of databases/ and it should work. Technically, index.html is not required either, so you could just use library-database-demo-section.


Only top voted, non community-wiki answers of a minimum length are eligible