如何在OctoberCMS的Plugin boot方法中获取无组件/片段代码的CMS页面解析内容
Got it, let's walk through how to get parsed CMS page content without those component/partial tags right in your plugin's boot method. I've tackled similar scenarios before, so here's a step-by-step solution that should work for you:
Step 1: Fetch the CMS Page Instance
First, you need to grab the target CMS page using its slug (or any identifier you prefer). In your plugin's boot method, use the Cms\Models\Page model to query it:
public function boot() { // Replace 'your-target-slug' with the actual slug of your CMS page $targetPage = \Cms\Models\Page::where('slug', 'your-target-slug')->first(); if (!$targetPage) { // Handle the case where the page doesn't exist \Log::warning('Target CMS page not found!'); return; }
Step 2: Filter Out Component & Partial Tags
The raw CMS page content lives in the content field, which includes all the Twig tags. We'll use a regex to strip out any {% component %} or {% partial %} blocks (even if they span multiple lines):
// Get the raw Twig content from the page $rawContent = $targetPage->content; // Regex to remove component and partial tags (handles multi-line blocks) $filteredContent = preg_replace('/{%\s*(component|partial)\s+.*?%}/s', '', $rawContent);
Step 3: Parse the Filtered Content with Twig
Now we need to parse the cleaned-up content using October's Twig environment to get pure HTML. Make sure to pass in any variables the page relies on (like the page itself, or global variables):
// Retrieve October's Twig environment instance $twig = \App::make('twig'); try { // Render the filtered content with necessary variables $pureParsedHtml = $twig->render($filteredContent, [ 'page' => $targetPage, // Add any other variables your page uses (e.g., 'site' for global site vars) ]); // Now $pureParsedHtml is your desired pure HTML, no component/partial tags left! // Do whatever you need with it here—log it, store it, pass it to another service, etc. \Log::info('Parsed pure HTML content:', ['content' => $pureParsedHtml]); } catch (\Twig\Error\Error $e) { // Handle any Twig parsing errors \Log::error('Failed to parse filtered CMS content:', ['error' => $e->getMessage()]); } }
Important Notes
- Regex Limitation: The regex above works for most standard component/partial tags, but if you have nested Twig tags inside those blocks (e.g., a component tag with an
{% if %}inside), it might not catch everything. For more complex cases, you'd need a Twig token parser to properly strip those nodes, but this regex is a solid starting point. - Variable Context: Make sure to pass all variables that your page's content uses. If the page references
{{ page.title }}or global vars like{{ site.name }}, include those in the render array to get accurate parsed content. - Boot Method Timing: Since the boot method runs after all service providers are registered, you don't have to worry about the CMS or Twig services not being available. If you run into issues, you could also hook into an event like
cms.page.beforeDisplay, but the boot method should work fine for most use cases.
内容的提问来源于stack exchange,提问作者Zakir hussain

