Mo'ylovli Shablonlar shablonni kengaytirishi mumkinmi?

Mo''jiz uchun yangi odamman.

Ko'pgina templating tillari (masalan, Django / Jinja ) siz kabi "ota-ona" shablonini ...

base.html

<html><head></head>
    <body>
    {% block content %}{% endblock %}
    </body>
</html>

frontpage.html

{% extends "base.html" %}
{% block content %}

Foobar!

{% endblock %}

oldpage.html ko'rsatildi

<html><head></head>
    <body>
    

Foobar!

</body> </html>

I'm aware of Mustache's partials (e.g., {{>content}}), but those seem to be just includes.

Mo'ylov uchun shablon kengaytmasi mavjudmi? Yoki, bu muvaffaqiyatsiz bo'lsa, kamida, o'z ichiga olgan shablonni kengaytma ekvivalentlariga aylantirgan bir necha dizayn naqsh mavjud.

51

8 javoblar

Yaqinda men bir qayiqda o'zimni topdim.

Mo'ylov shablon kengaytmasi/merosiga ruxsat bermaydi, ammo bilishim mumkin bo'lgan bir nechta variant mavjud.

  1. You could use partials:

    {{>header}}
        Hello {{name}}
    {{>footer}}
    
  2. You could inject template pre-processing functions into the context for each template that needs to inherit from some other page:

    {{#extendBase}}      
        Hello {{name}}
    {{/extendBase}} 
    

    Hash:

    {
       "name": "Walden",
       "extendBase": function() {
           return function(text) {
               return "<html><head></head>" + render(text) + "</body></html>"
           }
       }
    }
    
  3. Prepend and append the desired HTML to the relevant pages in your controller.

  4. Have a layout template ala:

    {{>header}}
        {{{body}}}
    {{>footer}}
    

    And render the body in your controller, passing that to the layout template as a variable named body.

  5. Implement template inheritance, pre-mustache, in your code that loads templates.

I wouldn't, however, use the triple mustache because I don't want unescaped HTML to be appearing anywhere, it's just too risky in my opinion.

If someone else has a better solution to this problem I'd love to hear it as well, since I haven't yet taken the plunge in any one of these directions.

59
qo'shib qo'ydi
U holda, PHP-da foydalanish uchun qisman o'xshashmi?
qo'shib qo'ydi muallif Alvaro, manba
# 4 "uch mo'ylov bo'lishi kerakmi?" - {{body}}}
qo'shib qo'ydi muallif Chris W., manba
Ha, afsuski, siz mening uchun # 4 qoidasini bekor qilganingiz ma'qul. Javobni yangiladim.
qo'shib qo'ydi muallif Walden, manba
Va nihoyat, qayta ishlatiladigan pastki shablonlarni yaratish va shablonni yuklash kodida merosni qo'llash (umuman # 5) umuman ko'proq moslashuvchan va ishonchli bo'ladi. Andoza namunasi bilan bog'liq muammo shundaki, agar tashqi shablonni yozsangiz, siz hech qachon bola shablonida talab qilinadigan narsalarni yaxshi bilishmaydi (yoki talab qilinmaydi). Bu ko'pincha tashqi shablonda muayyan bir bola sahifasi uchun kerak bo'lishi mumkin bo'lgan (lekin boshqasi uchun keraksiz) JS/CSS-ni o'z ichiga oladi. Shablonni merosdan foydalanganda bu muammo atrofidagi yagona yo'l JS/CSS talablarini o'zgarmaydigan qilib o'tishdir.
qo'shib qo'ydi muallif Walden, manba
qo'shib qo'ydi muallif Walden, manba
(@Walden Ro'yxatingizga 0 yoki 6 raqamini qo'shib, Hoogian haqida so'zlab berishingiz mumkinmi? Fikrni qo'shganingiz uchun rahmat :-))
qo'shib qo'ydi muallif KajMagnus, manba

Men buni bu yerdagi mo'ylov uchun spetsifikatsiyaga taklif qildim:

https://github.com/mustache/spec/issues/38

Hozirda mustache.java, hogan.js va phly_mustache qo'llab-quvvatlovchi shablonni meros.

12
qo'shib qo'ydi
Hogan.js bilan bog'liq bo'lsa-da, uni hujjatlardan bilib olmaysiz: github.com /twitter/hogan.js/issues/70
qo'shib qo'ydi muallif Paul D. Waite, manba
GRMustache ham shunday.
qo'shib qo'ydi muallif Gwendal Roué, manba

Mo'ylov shablon kengaytmasi qilmaydi.

Agar chindan ham shablon kengaytmasini xohlasangiz, sizga bu funktsiyali siz tanlagan til/ramka uchun yaratilgan kutubxona maqsadidan foydalanishingiz mumkin.


FYI, I'm using Node.js/Express, so I will probably end up using https://github.com/fat/stache

3
qo'shib qo'ydi
V Stash saqlanmaydi va taklifni almashtirish hogan.js kengaytirilmaydi.
qo'shib qo'ydi muallif mikemaccana, manba
Twitterdagi Hoogian endi merosni qo'llab-quvvatlaydi. Bu yaqinda tuzilgan majburiyatga qarang: Hogan 3. Andoza namunasini qo'shish ...
qo'shib qo'ydi muallif KajMagnus, manba

Mo'ylov php-da, shablonni meroslash versiya 2.7.0 dan boshlab qo'llab-quvvatlanadi.

https://github.com/bobthecow/mustache.php/wiki/BLOCKS-pragma

Mavjud versiyasini Mustache/Engine.php faylidan topishingiz mumkin va quyidagi satrlarni qidirishingiz mumkin:

class Mustache_Engine
{
    const VERSION        = '2.8.0';
    ...
3
qo'shib qo'ydi

HTMLni o'z ichiga olgan parametrlardan foydalanishingiz mumkin. {{variable}}} kabi "uch mo'ylovi" asta-sekin HTMLni qaytaradi. Shablon kengaytmasi bilan bir xil emas, lekin siz frontpage-content.html ni yaratishingiz va keyin bazaga uzatiladigan content o'zgaruvchisiga chiqishingiz mumkin. html .

(Bunday nom berish naqshlari fayl nomlarini boshqarishga yordam berishini kutish bilan -content frontpage.html fayliga qo'shdim.)

3
qo'shib qo'ydi

Men hozir Pythonda (men Mako yaratuvchisi hisoblayman) bu erda o'ynab o'ynayapman, shuning uchun bo'limlarni ushlab turgan dinamik kontekstda to'g'ri ish qilayotgan ko'rinadi, garchi bularni sinab ko'rish kerak bo'lsa ham.

Basically we are using lambdas, where a "<" prefix indicates "inherit from this template" (similar to the syntax discussed at https://github.com/mustache/spec/issues/38) and a "$" prefix indicates "this is an inherited section".

import pystache

class NameSpace(object):
    def __init__(self, renderer, vars_={}):
        self.renderer = renderer
        self._content = {}
        self.vars = vars_

    def add_content(self, name, value):
        self._content[name] = value

    def __getattr__(self, key):
        if key in self.vars:
            # regular symbol in the vars dictionary
            return self.vars[key]
        elif key.startswith("<"):
            # an "inherit from this template" directive
            name = key[1:]
            return inheritor(self, name)
        elif key.startswith("$"):
            # a "here's a replaceable section" directive
            name = key[1:]
            if name in self._content:
                # if we have this section collected, return the rendered
                # version
                return sub_renderer(self, name)
            else:
                # else render it here and collect it
                return collector(self, name)
        else:
            # unknown key.
            raise AttributeError(key)

def sub_renderer(namespace, key):
    def go():
        def render(nested):
            return namespace._content[key]
        return render
    return go


def collector(namespace, key):
    def go():
        def render(nested):
            content = namespace.renderer.render(nested, namespace)
            namespace.add_content(key, content)
            return content
        return render
    return go


def inheritor(namespace, name):
    def go():
        def render(nested):
            namespace.renderer.render(nested, namespace)
            return namespace.renderer.render_name(name, namespace)
        return render
    return go

Shunday qilib, ba'zi shablonlar mavjud. base.mustache:

<html>

{{#$header}}
    default header
{{/$header}}

{{#$body}}
    default body
{{/$body}}

{{#$footer}}
    default footer, using {{local key}}
{{/$footer}}


</html>

hello.mustache:

{{#

va keyin chuqur uch darajali o'ynash uchun, subhello.mustache:

{{#

Quyidagi kabi hello.mustache:

renderer = pystache.Renderer(search_dirs=["./templates/"])

print renderer.render_name("hello",
                    NameSpace(renderer, {"local key": "some local key"}))

chiqdi:

<html>

    new header

    new body, with some local key

    default footer, using some local key


</html>

Subhello.mustache:

print renderer.render_name("subhello",
                    NameSpace(renderer, {"local key": "some local key"}))

chiqdi:

<html>

    new header

    new body, with some local key

    im some new footer


</html>

Men buni yigirma daqiqada yozgan edim, men faqat handlebars.js dan o'tmishda va dastlabki paytlarda biroz foydalanardim, hozir esa butun "mo'ylov" fikri men uchun chuqur emas. Lekin, bu ish kabi ko'rinadi?

1
qo'shib qo'ydi

Agar siz server tomonidan faqatgina kod bilan xursand bo'lsangiz, Nun - bu Bo'stonga o'xshash templating tizimi, shabloni bekor qiladigan xususiyatni - djangoga modellangan. Biroq, u ishlayotgan bo'lsa-da, uning muallifi uni ushlab turmaydi.

0
qo'shib qo'ydi
Python
Python
372 ishtirokchilar

Bu guruh python dasturlash tilini muhokama qilish uchun. Iltimos, o'zingizni hurmat qiling va faqat dasturlash bo'yicha yozing. Botlar mavzusini @botlarhaqida guruhida muhokama qling! FAQ: @PyFAQ Offtopic: @python_uz_offtopic

Python offtopic group !
Python offtopic group !
150 ishtirokchilar

@python_uz gruppasining offtop gruppasi. offtop bo'lsa ham reklama mumkin emas ) Boshqa dasturlash tiliga oid gruppalar @languages_programming

Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb