Giter Site home page Giter Site logo

layouts's Introduction

Kirby Layouts plugin

This plugin extends Kirby’s templates with a powerful layout system.

Installation

Download

Download and copy this repository to /site/plugins/layouts.

Git submodule

git submodule add https://github.com/getkirby/layouts.git site/plugins/layouts

Composer

composer require getkirby/layouts

How it works

You can create full HTML layouts in a new /site/layouts folder. Layouts can define slots, which will then be filled with content by templates.

Layouts

You can create as many layouts as you need in your /site/layouts folder. Start with a default.php layout that will be picked up if no layout name is specified in the template.

/site/layouts/default.php

<html>
    <head>
        <title><?= $page->title() ?></title>
    </head>
    <body>
        <?php slot() ?>
        <?php endslot() ?>
    </body>
</html>

/site/templates/my-template.php

<?php layout() ?>

<h1>Hello world</h1>
<p>This will end up in the default slot</p>

Choosing a layout

To use a specific layout, you can pass its name to the layout() method.

/site/layouts/blog.php

<html>
    <head>
        <title>Blog</title>
    </head>
    <body>
        <h1>Blog</h1>
        <?php slot() ?>
        <?php endslot() ?>
    </body>
</html>

/site/templates/blog.php

<?php layout('blog') ?>

<!-- some articles -->

Working with slots

You can add as many different slots to your layout as you need. The default slot goes without a specific name. Every other slot needs a unique name. Slots in layouts can define default content, which will be rendered if the slot is not used in the template.

/site/layouts/default.php

<html>
    <head>
        <?php slot('head') ?>
        <title>Blog</title>
        <?php endslot() ?>
    </head>
    <body>
      <div class="page">

        <header class="header">
          <a href="/">Logo</a>
        </header>

        <div class="sidebar">
          <?php slot('sidebar') ?>
          <!-- default sidebar setup -->
          <?php endslot() ?>
        </div>

        <main class="main">
          <h1>Blog</h1>
          <?php slot() ?>
          <!-- this is the default slot -->
          <?php endslot() ?>
       </main>
     </div>
  </body>
</html>

Once the slots are defined, you can fill them from your template. If you use multiple slots, you must wrap content for each slot in the slot and endslot methods.

/site/templates/blog.php

<?php slot('sidebar') ?>
<nav>
  <!-- a custom sidebar menu -->
</nav>
<?php endslot() ?>

<?php slot() ?>
<!-- html for the default slot -->
<?php endslot() ?>

Working with snippets

Kirby's template system stays exactly as you know it. You can still work with templates without layouts and you can also still use snippets – in your templates and in your layouts.

/site/layouts/default.php

<html>
    <head>
        <?php slot('head') ?>
        <title>Blog</title>
        <?php endslot() ?>
    </head>
    <body>
      <div class="page">

        <?php snippet('header') ?>

        <div class="sidebar">
          <?php slot('sidebar') ?>
          <?php snippet('sidebar') ?>
          <?php endslot() ?>
        </div>

        <main class="main">
          <h1>Blog</h1>
          <?php slot() ?>
          <!-- this is the default slot -->
          <?php endslot() ?>
       </main>
     </div>
  </body>
</html>

Global layout data

You can pass global data to the layout method and make it available in every slot and snippet of your layout.

/site/layouts/default.php

<html>
    <head>
        <?php slot('head') ?>
        <title><?= $title ?></title>
        <?php endslot() ?>
    </head>
    <body>
      <div class="page">

        <?php snippet('header') ?>

        <div class="sidebar">
          <?php slot('sidebar') ?>
          <?php snippet('sidebar') ?>
          <?php endslot() ?>
        </div>

        <main class="main">
          <h1>Blog</h1>
          <?php slot() ?>
          <!-- this is the default slot -->
          <?php endslot() ?>
       </main>
     </div>
  </body>
</html>

/site/templates/blog.php

<?php layout('blog', ['title' => 'Blog') ?>

Some more content ...

License

MIT

Credits

layouts's People

Contributors

bastianallgeier avatar afbora avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.