定制 lameco/craft-twig-components 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lameco/craft-twig-components

Composer 安装命令:

composer require lameco/craft-twig-components

包简介

README 文档

README

  • Craft CMS 5.0+
  • TailwindCSS 4.0+
  • AlpineJS 3.0+
  • PHP 8.4+

Steps to use the plugin

  1. Define the themeConfig
{% set ComponentsThemeConfig = {
  gsapAnimations: false,
  
  ui: {
    
    breadcrumbs: {
        class: "",
        home: {
            html: ""
        },
        back: {
            class: "",
            html: ""
        },
        separator: {
            html: ""
        },
        item: {
            class: "",
            current: {
                class: ""
            }
        }
    },
    
    button: {
        class: "",
        overlay: {
            class: "",
        },
        type: {
            [primary|primaryDark|secondary|secondaryDark|text|textDark]: {
                class: "",
                overlay: {
                    class: "",
                },
                icon : {
                    class: "",
                    html: "",
                }
            }        
        }
        inner: {
            class: ""
        },
        icon : {
            position: "[start|end]",
            class: "",
            html: ""
        }
    }
    
    collapse: {
        class: "",
        button: {
            class: "",
            title: {
                class: ""
            },
            icon: {
                class: "",
                html: "",
                openHTML: "",
                closeHTML: ""
            }
        },
        content: {
          class: ""
        }
    },

    image: {
      picture: {
        class: ""
      },
      img: {
        class: "",
        mobileBreakpoint: ""
      },
      caption: {
        class: ""
      }
    },
    
    modal: {
      class: "",
      overlay: {
        class: ""
      },
      content: {
        class: ""
      },
      title: {
        class: ""
      },
      button: {
        class: "",
        html: ""
      }
    },
  
    video: {
      mode: "[embed|lightbox]",
      embed: {
        class: ""
      },
      lightbox: {
        class: "",
        button: {
          class: "",
          html: ""
        }
      },
      consent: {
        class: ""
      }
    },
  },
  
  social: {
    follow: {
       class: "",
       text: "",
       items: [ "facebook", "linkedin", "twitter", "instagram", "pinterest", "youtube", "vimeo" ],
       list: {
        class: "",
       },
       item: {
        class: "",
       }
    },
    
    share: {
       class: "",
       text: "",
       items: [ "copy", "mail", "linkedin", "facebook", "twitter", "whatsapp" ],
       list: {
        class: "",
       },
       item: {
        class: "",
       }
    }
  }
  
  sprig: {
    loader: {
        class: "",
        html: ""
    },
    
    pagination: {
        class: "",
        neighbours: 1,
        list: {
            class: "",
        },
        item: {
            class: "",
            active: {
                class: ""
            },
        },
        arrows: {
            class: "",
            disabled: {
                class: ""
            },
            prev: {
                html: ""
            },
            next: {
                html: ""
            },
        },
        ellipsis: {
            class: "",
            html: ""
        }
    }
  }
} %}
  1. Add the themeConfig to the _globals so they are available most of the time.

If you are using Sprig for example the themeConfig will not be available in the Sprig templates. You can pass the themeConfig (or a portion of the stripped down themeConfig) to the Sprig template using the sprig function and passing the theme object into the theme attribute of a component.

Be sure to | json_encode the themeConfig when passing it to Sprig, because Sprig only allows string, boolean, numbers or arrays.

{{ sprig('path/to/template', { theme: ComponentsThemeConfig | json_encode }) }}
{% do _globals.set('lameco.components.theme', ComponentsThemeConfig) %}
  1. Register the assets to make sure the plugin's styles and scripts are loaded in your templates:
{% do view.registerAssetBundle(
'lameco\\crafttwigcomponents\\assetbundles\\CraftTwigComponents\\CraftTwigComponentsAsset'
) %}
  1. Include the Twig Components SCSS in your main SCSS file.
@use "../../../vendor/lameco/craft-twig-components/src/assets/scss/index";
  1. Add the Twig Components template source to SCSS.
@source "../../../vendor/lameco/craft-twig-components/**/*.twig";

FontAwesome

The Component Library uses FontAwesome for icons used in fallback logic if nothing in the themeConfig is defined. In the fontawesome.ts file import the pre-defined icons using:

import { twigComponentsFontAwesomeIcons } from "../../../vendor/lameco/craft-twig-components/src/assets/ts/fontAwesome";

library.add(
    ...twigComponentsFontAwesomeIcons,
    // other icons you want to add
)

AlpineJS

The Component Library uses AlpineJS for the interactive components. Make sure to include AlpineJS in your project and initialize it properly. At least the following AlpineJS plugins are required:

  • collapse

Sprig

The Component Library uses Sprig for dynamic components. Make sure to include Sprig in your project and initialize it properly.

Pagination

Since the pagination is a Sprig component, you need to pass the themeConfig to the Sprig template. You can do this by passing the theme attribute to the Sprig component:

{{ sprig('path/to/pagination', { theme: ComponentsThemeConfig }) }}

Then when rendering the pagination, you can access the themeConfig in the Sprig template like this:

<x-sprig.pagination pageInfo="..." theme="{{ theme | json_encode }}"></x-sprig.pagination>

lameco/craft-twig-components 适用场景与选型建议

lameco/craft-twig-components 是一款 基于 Twig 开发的 Composer 扩展包,目前已累计 2.7k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 06 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 lameco/craft-twig-components 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lameco/craft-twig-components 我们能提供哪些服务?
定制开发 / 二次开发

基于 lameco/craft-twig-components 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.7k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: Twig

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-18