LoginSignup
1
0

More than 5 years have passed since last update.

DjangoのtemplateとLaravelのbladeの構文をちょっと比較してみた

Posted at

まとめ

  • 両者非常に似通っていて、同じ感覚で使用できる

  • Laravelでは親の@yieldに子の@sectionが展開されるが、Djangoでは親子ともにblockで表現する

  • Laravelでは、@extendslayouts/app.blade.phpを使用する際に、layouts.appと指定する(layouts/appではない)のが少し戸惑った

Djangoの場合

親のテンプレート

templates/base.html
<!DOCTYPE html>
<html>
<head>
  ...
</head>
<body>
  <header>
    ...
  </header>
  <main>
    {% block content %}
    {% endblock %}
  </main>
  <footer>
    ...
  </footer>
</body>

子のテンプレート

templates/hoge/fuga.html
{% extends 'base.html' %}

{% block content %}
<div>
  {% if items %}
    <ul>
       {% for item in items %}
         <li>{{ item }}</li>
       {% endfor %}
    </ul>
  {% endif %}
</div>
{% endblock %}

Laravelの場合

親のテンプレート

resources/views/layouts/app.blade.php
<!DOCTYPE html>
<html>
<head>
  ...
</head>
<body>
  <header>
    ...
  </header>
  <main>
    @yield('content')
  </main>
  <footer>
    ...
  </footer>
</body>

子のテンプレート

resources/views/hoge.blade.php
@extends('layouts.app')

@section('content')
<div>
  @if (count($items)) > 0)
    <ul>
       @foreach( $items->all() as $item)
         <li>{{ $item }}</li>
       @endforeach
    </ul>
  @endif
</div>
@endsection
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0