twigのimportとincludeはどこが違うの? (6レス)
上下前次1-新
1: 07/10(水)18:51 ID:G1nCEzbNC(1/6) AAS
{% import 'custom_macros.twig' as macros %}
{# {% macro alert(type, message) %}<div class="alert alert-{{ type }}">{{ message }}</div>{% endmacro %}
{% macro link(url, text) %}<a href="{{ url }}">{{ text }}</a>{% endmacro %} #}
{{ macros.alert('success', '操作が成功しました') }}
{{ macros.link('外部リンク:example.com', 'Visit Example') }}
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ site_title }}</title><style>
body {font-family: Arial, sans-serif;line-height: 1.6;margin: 20px;}
header, nav, main, footer {margin-bottom: 20px;padding: 10px;
省12
2: 07/10(水)18:52 ID:G1nCEzbNC(2/6) AAS
<?php
require_once 'vendor/autoload.php';$loader = new \Twig\Loader\FilesystemLoader(__DIR__);
$twig = new \Twig\Environment($loader);$siteTitle = 'Fake Site';
$loremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
$services = ['Web Development', 'Graphic Design', 'SEO Optimization'];
$currentYear = date('Y');
class Router {protected $routes = [];public function get($route, $callback) {
$this->routes['GET'][$route] = $callback;}
public function dispatch() {$method = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];if (isset($this->routes[$method][$uri])) {
省8
3: 07/10(水)18:54 ID:G1nCEzbNC(3/6) AAS
import→変数などを引き継ぎ代入する
include→文章をそのまま代入シたり
htmlのタグなどを使う時に
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>タヌキのフェイクゲーム</title><style>
body {font-family: Arial, sans-serif;text-align: center;background-color: #f0f0f0;}
.tanuki-img {max-width: 300px;margin: 20px auto;display: block;}
.message {background-color: #fff;padding: 10px;border: 1px solid #ccc;
border-radius: 5px;max-width: 80%;margin: 20px auto;}</style></head><body>
省13
4: 07/10(水)19:31 ID:G1nCEzbNC(4/6) AAS
******twig_base******
<!DOCTYPE html><html lang="ja"><head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="styles.css"></head><body><header>
<nav><ul><li><a href="/">ホーム</a></li><li><a href="/products">商品一覧</a></li>
<li><a href="/cart">カート</a></li></ul></nav></header>
<main>{% block content %}{# ページ固有のコンテンツをここに書く #}{% endblock %}</main>
<footer>© {{ 'now'|date('Y') }} ECサイト. All Rights Reserved.</footer></body></html>
******twig_main******
{% extends 'base_main.twig' %}{% block title %}商品一覧 - ECサイト{% endblock %}
省6
5: 07/10(水)19:32 ID:G1nCEzbNC(5/6) AAS
******php******
<?php
require_once 'vendor/autoload.php';$loader = new \Twig\Loader\FilesystemLoader(__DIR__);
$twig = new \Twig\Environment($loader);
$products = [['name' => '犬', 'description' => 'dog.jpg', 'price' => '1000', 'decorations' => ['Daiya', 'Asta','Star']],
['name' => '猫', 'description' => 'cat.jpg', 'price' => '1200', 'decorations' => ['Daiya', 'Asta','Star']],
['name' => 'ウサギ', 'description' => 'rabbit.jpg', 'price' => '2000', 'decorations' => ['Daiya', 'Asta','Star']],];
echo $twig->render('layout.twig', ['products' => $products,]);
?>
6: 07/10(水)20:37 ID:G1nCEzbNC(6/6) AAS
{% set max_attempts = 3 %}<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8">
<title>Escape Game</title></head><body><h1>Escape Game</h1><p>Guess the secret number between 1 and 10!</p>
{# {{secret_number}} #}{% block content %}{% set attempts = attempts + 1 %}{% if attempts > max_attempts %}
<p>Sorry, you failed to guess the number within {{ max_attempts }} attempts.</p>
<p>The secret number was {{ secret_number }}.</p>{% endif %}
<form action="?attempts={{attempts}}&secret_number={{secret_number}}" method="post">
<label for="guess">Enter your guess (1-10):</label>
<input type="number" id="guess" name="guess" min="1" max="10" required>
<button type="submit">Guess</button></form>{% if guess == secret_number %}
<p>Congratulations! You guessed the secret number {{ secret_number }}!</p>{% else %}
省12
上下前次1-新書関写板覧索設栞歴
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル
ぬこの手 ぬこTOP 0.262s*