We use cookies to make your viewing experience better. By accepting you consent, you agree to our Cookie policy

Accept
Improve your Craft CMS skills

How To Access The Templates Directory On Craft CMS

10 min read
Shape April 2022 HR 28

Managing templates is a core part of building sites in Craft CMS. But all that power won't help if you can't find the right template file when you need it. This article will show you how to expertly navigate the Craft templates directory, find any template quickly, and take total control of your frontend code. You'll learn the template structure, access templates fast, and eliminate frustrating hunting for files. Read on to boost your productivity editing Craft templates.

To access the templates directory in Craft CMS, go to yourdomain.com/admin and sign in. Click "Templates" in the left nav. Template files are organized into folders like _pages and _partials. Open any file by clicking on it. Use the category tabs to filter templates. Click the "+" icon to create new templates. Manage permissions under a template's Settings. Enable Git version control for better collaboration.

Logging into the Craft CMS Control Panel

Accessing the Login Page

The control panel is the administrative interface where site managers access all the powerful features of Craft CMS. To get to the control panel, you'll need to navigate to the login page.

The default URL for logging into a Craft CMS site is www.yourdomain.com/admin. However, the developers may have customized the admin path, so check with them if you're not sure of the exact URL.

When you navigate to the admin login page, you'll see a simple login form prompting you for a username and password. These admin credentials are set up when Craft is installed, so check with the site owner if you don't have them.

Without valid credentials, you won't be able to access the control panel and all its robust site management capabilities. So getting the right admin username and password is crucial.

Entering Admin Credentials

On the login screen, enter the correct admin username and password to gain access to the control panel. These credentials are case-sensitive, so make sure caps lock is off and enter them exactly as given to you.

If the login fails, carefully check that you're using the right admin username and didn't mistype the password. Also verify that your keyboard language/layout is set correctly if the password contains special characters.

If you're certain the credentials are correct but the login still fails, the admin account may have been locked due to too many failed attempts. Contact the site owner, who can unlock the account.

You can also use the "Forgot your password?" link to reset the password if you can no longer remember it or don't have it.

Resetting a Forgotten Password

If you forget your Craft CMS admin password, use the "Forgot your password?" link on the login page.

This will take you to a password reset screen where you can enter the admin email address. An email will be sent with a special reset token link. Clicking the link will take you to a reset password form.

Enter and confirm a new secure password to complete the reset. You can then login with the new password.

The reset link expires after 24 hours, so make sure to reset the password soon after receiving the email. If the link has expired, go back to the login screen and repeat the forgot password process to generate a fresh reset token.

Resetting a lost admin password is easy and lets you get back into the control panel quickly. Just be sure to save the new password in your password manager so you don't forget it again!

The admin credentials allow full access to the Craft CMS control panel, so keep them secure. Reset forgotten passwords promptly. With the right username and password, you'll enjoy the full power of managing a Craft site.

Navigating to the Templates Menu

Finding the Templates Tab

After logging into the Craft CMS control panel, you'll see the main navigation menu on the left side of the screen. This provides access to all the different sections of Craft.

Look for the "Templates" tab in the main navigation menu. Clicking this tab will take you to the Templates section. This is where all the templates for the Craft site are organized and managed.

The Templates menu item may be towards the top or bottom of the main nav, depending on how the Craft install was configured. If you don't see it, scroll through the menu carefully as it should be there.

Knowing how to find the Templates tab quickly is essential, as this is the gateway to managing all front-end templates in Craft.

Understanding Template Sections

Within the Templates section, you'll see all the actual templates organized into different sections. These include:

  • Globals - Global templates and layouts that apply site-wide.

  • Pages - Templates for frontend pages and entries.

  • Partials - Reusable template snippets and components.

  • Emails - Templates for transactional emails.

Understanding what these sections contain helps you navigate the Templates area. For example, if you need to update a homepage template, look in Pages. For header/footer templates, check Partials.

There may also be custom sections like Blog, News, etc. depending on how the templates were organized for the site.

Using the Category Tabs

To help find templates faster, use the Category tabs running along the top of the Templates section.

These tabs allow filtering the templates by type. For example, click the Pages tab to only see page templates. Or click the Emails tab to only see email templates.

The All tab shows everything. And the Recent, Modified, and Mine tabs help filter based on edit activity.

Using the template category tabs is the best way to quickly locate a specific template when you know the general type you are looking for. They help avoid scrolling through tons of irrelevant templates.

With the Templates menu and categories mastered, navigating Craft templates becomes easy. You'll be able to jump right to the template you need and reduce frontend editing time when using the Craft CMS control panel.

Understanding the Templates Directory Structure

The Craft CMS templates are organized into a structured directory system to keep things logical and easy to navigate. Getting familiar with the key folders and organization will help you quickly find the template you need.

Key Top-Level Directories

At the top level of the templates section are some key folders that serve important purposes:

  • _layouts - Contains global layout templates that all other templates extend from. This includes things like header, footer, and sidebar templates.

  • _macros - Houses reusable macro templates that get included in other templates using {% include '' %} tags. Macros are for repeatable chunks of code.

  • _pages - Where page templates live. For example, frontpage.html and about.html would be in here. This folder corresponds to frontend URIs.

Understanding the purpose of these top-level folders helps orient you as you dig into the templates structure.

Section and Channel Folders

Within the _pages folder, you may see additional folders named after different sections and channels.

For example, there may be a news folder for news section entries, a blog folder for blog entries, etc. This keeps templates organized based on the content type.

The section and channel folders contain the templates used to display those content entry types on the frontend of the site.

Understanding the _partials Folder Structure

The _partials folder contains all reusable template partials and components. To keep things organized as this folder grows, partials are grouped into subfolders by type and functionality. For example, there may be a navigation subfolder for navigation/menu partials, a footer subfolder for footer partials, etc. This categorizes partials for easy discovery.

There could also be folders for partials related to specific plugins or custom modules. Understanding how _partials are structured saves time hunting down a specific partial template.

Learning the templates file structure and directories gives you the lay of the land. You'll gain confidence navigating and locating templates efficiently using the control panel in Craft CMS.

Viewing and Opening Template Files

Once you've navigated to the Templates section in the Craft control panel, you'll want to actually view and access the templates to make frontend changes. Here's how that works.

Accessing Templates in the Control Panel

In the Templates section, you'll see a visual file/folder structure representing the template organization. Click on any file icon to open and view the code for that template.

For example, clicking on home.twig in the Pages folder will open the homepage template file in the editor panel. You can then view or edit the template code.

Double-clicking a template file will also open it. And you can right-click files to access delete and rename functions. Navigating the Templates section works similar to browsing files on your computer.

Template File Types

Craft CMS supports different template file types and extensions including:

  • .twig - Twig templating language files. This is the most common template type.

  • .html - Straight HTML template files.

  • .json - Stores template meta information.

Knowing which file extension a template uses helps understand its purpose. For example, .html files contain pure HTML while .twig files support Twig syntax.

Some templates like emails may even use .txt for plain text formatting. The file extension matches the coding language.

Previewing Templates

When you edit and save changes to a template in the control panel, those changes won't immediately go live on the frontend.

Use the Preview function to test how your template edits will look on the live site. Preview opens the page in a new browser tab.

The Preview feature is useful for catching any issues before pushing template changes live. It ensures the edits look and function as expected.

Craft CMS makes it easy to access, view, and test templates right from the friendly control panel interface. In just clicks, you can open files, make changes, and preview the results.

Creating New Templates

Adding new templates is a common task in Craft CMS. Here's how to easily create new template files right from the control panel.

Adding Templates

To add a new template, navigate to the Templates section and click the "+" icon. This will bring up the new template form.

Give your template a name - something descriptive like "blog-post" or "product-page". Avoid vague names like "new-template".

Select the template type/file extension. ".twig" is best for full templates using Twig syntax.

Choose where to save the template in the existing directory structure. Organizing up front saves hassle later.

Hit "Save" and the new .twig template will be created and ready for coding! It's that easy to add new templates with Craft.

Organizing Template Files

When adding new templates, take a moment to consider where they should live within the directory structure.

Keep things logically organized - for example, blog post templates in a "blog" folder, landing pages inside "_pages", etc.

Be consistent with existing conventions. If product pages are already in a "products" subfolder, continue that pattern.

Good organization may take a few extra seconds now, but saves tons of time when managing a large site build.

Template Type and Filename

When creating a new template file in Craft CMS, pay attention to the template type and filename:

  • Choose .twig for robust templates using Twig syntax and features.

  • .html for basic static HTML templates with no dynamic code.

  • .json for template meta info files.

Then name the file something short but descriptive like blog-post.twig or product-page.html.

Follow naming conventions already used for consistency. The filename should match the template content.

With these tips, you'll be creating new Craft templates quickly and keeping them neatly organized. Just click "+" , name the file smartly, choose the type, and start building!

Editing Existing Templates

Modifying and updating existing templates is a key part of managing a Craft CMS website. Here's how to edit templates from the control panel.

Accessing the Template Editor

To edit an existing template, navigate to the Templates section and open the template file you want to modify. Double click the file or click the name.

This will open the file in the template editor panel. You'll see the Twig code on the left side and a rendered preview on the right side.

The editor allows making changes while previewing how those changes look live. Everything can be edited directly within the control panel.

Making Template Changes

With the template open in the editor, you can start making changes:

  • Update text and markup throughout the template code.

  • Add, remove, or modify any of the Twig tags and syntax.

  • Revise the overall template structure and components.

  • Adjust any HTML, CSS, or JavaScript as needed.

As you edit, the preview panel will dynamically update to reflect your changes. This allows fine-tuning the edits until they look perfect.

When done, click "Save" to save the changes. The edits won't go live until the template is re-published.

Editing Template Code

The template editor makes it easy to modify the Twig code and HTML/CSS markup within a template file.

Use the editor tools to find, replace, and format code. Insert new tags and variables as needed. Rearrange code sections by dragging and dropping.

For heavy markup edits, toggle to the HTML view using the buttons at the bottom. This provides a raw HTML editing experience.

The editor is flexible enough for tweaking text or completely overhauling the template code. With craft templates, you can efficiently edit at both high levels or down in the code weeds.

Making template changes is a key site management task. Craft CMS streamlines it with a powerful editor right in the control panel. Editing templates has never been easier.

Including Template Partials

Template partials allow reusable chunks of code to be created once then included anywhere. Here's how to work with partials in Craft CMS.

Creating Reusable Partials

A template partial is a lightweight sub-template that gets included in other templates.

For example, a header, footer, sidebar, or blog post summary could be a partial. Any reusable section of code.

To create a partial, add a new .twig template to the _partials folder. Name it like header.twig or post-summary.twig.

Build out the partial code - it can contain HTML, Twig, CSS, etc. Partials are flexible templates.

Once created, use the {% include %} tag to reuse the partial anywhere.

Organizing Partials

The _partials folder will fill up fast, so stay organized!

Group partials into subfolders like headers, footers, blogs, etc. This keeps things tidy.

Namespace partials appropriately as well. For example, components/_buttons.twig clearly belongs to components.

Consistency and structure will keep the partials library easy to navigate as it grows.

Including Partials in Templates

To include a partial, use the {% include '' %} tag pointing to the partial file:

{% include '_partials/headers/main.twig' %}

This embeds main.twig at that position in the template.

The include tag can be used multiple times to insert the partial in any template.

Partials are a powerful way to build reusable components in Craft CMS. Just create it once, include it anywhere, and keep things DRY.

Setting Template Permissions

Craft allows granular control over who can access and edit templates through user and group permissions.

User and Group Permissions

All templates have a permissions setting where you can choose which users or roles can view/edit that template.

For example, you may want Admins to have full access, while Editors can only update certain page templates.

Set this on a per-template basis by editing the template, going to Settings, and configuring the permissions.

Check the boxes for each user or group that should have permission to that template. Unlimited flexibility!

Granular Control

Craft provides very customizable template permissions.

Give view-only access to templates so users can see but not change the code.

Set broader permissions for groups like Editors, while limiting individual access.

Manage permissions for all templates globally or on a per-template basis.

Grant or deny access down to individual user and template level. Very granular!

Permission Types

The main template permissions are:

  • View - Allows viewing template code read-only.

  • Edit - Permits making changes and updating the template.

  • Delete - Grants ability to delete the template.

Additional action permissions may be available depending on plugins.

Set the right mix of permissions for each user/group to match their role. Allow editing blog templates for bloggers or view-only for legal pages, etc. Very customizable!

Leverage the robust permissions in Craft CMS to tightly control who can manage templates on a site. Keep your code secure!

Version Controlling Templates with Git

Git enables rock-solid version control and collaboration for Craft CMS templates.

Initializing a Repository

To start, initialize a new Git repository locally in the templates folder.

Run git init to create the repo. This turns the directory into a Git project to start tracking changes.

Add a remote repository as well on GitHub or Bitbucket to host the template code.

Now you have version control set up for templates to utilize commit history, branching, and more.

Cloning the Repository

On another environment like staging or production, clone down the template repository to sync the code.

Run git clone <repo> to download the repository containing the templates.

This pulls a complete copy of the template code so other environments stay in sync as changes are pushed.

Cloning the repo is the first step to deploying template updates to other servers.

Committing and Pushing Changes

As you edit templates locally, frequently commit changes:

git add .

git commit -m "Update homepage template"

This saves edits to the local Git history. Then when ready, push commits to the remote repo:

git push origin master

Now changes are version controlled both locally and on the remote repository.

With Git in place, template changes safely flow from development to production. No more manual syncing files!

Version control brings professional collaboration and deployment workflows to managing Craft CMS templates.

Troubleshooting Template Issues

Even the best Craft developers hit template problems occasionally. Here are some tips for debugging issues.

Fixing Broken Templates

If a template is throwing errors or not rendering properly:

  • Check the Craft logs for any Twig compilation errors pointing to issues.

  • Try previewing the faulty template and look for JS errors.

  • Enable debugging in config to see more helpful error messages.

  • Comment out sections of the template code to isolate the problem.

  • Compare to a known good template to spot anomalies.

With logical troubleshooting, most template bugs can be identified and fixed.

Debugging Variable Outputs

If a template variable isn't outputting as expected:

  • Use Twig's {{ dump() }} tag to inspect the variable's value.

  • Make sure the variable is defined and in scope where used.

  • Check for typos in the variable name. Case matters!

  • Try wrapping it in {{ ''|raw }} in case HTML encoding is interfering.

Slowly ruling out common issues will reveal the variable problem.

Identifying Conflicting Templates

If template changes don't seem to apply:

  • Look for identically named templates in different sections that may conflict.

  • Use the template path name like blog/_entry.twig to be specific.

  • Enable "Strict template names" in settings to avoid collisions.

  • Refactor similar templates into better namespaced partials.

Template conflicts often arise from unclear naming conventions. Avoid ambiguity!

With smart debugging tactics, even tricky template bugs can be quickly solved in Craft CMS.

Shape April 2022 HR 202
Andy Golpys
- Author

Andy has scaled multiple businesses and is a big believer in Craft CMS as a tool that benefits both Designer, Developer and Client. 

Share
Feedback
Show us some love
Email Us
We usually reply within 72 hours
Agency Directory
Submit your agency
Affiliate Partners
Let's chat