Skip to content
Grav 2.0 is officially stable. Read the announcement →
Support

Currently using PHP include in my site, any alternative in GRAV?

Started by iago 6 years ago · 3 replies · 687 views
6 years ago

Hi, I'm currently using include($_SERVER["DOCUMENT_ROOT"] . "/assets/table.html"); in my site, and I'm trying to move to grav. I know there's an injection plugin but somehow it only allows content under /user/pages and in markdown format.

I'll put some of this assets under the grav /asset pages because this is not content to be shown individually (a comparison table that I modify sometimes), but to be called into different articles and inserted manually.

Is there any way to work around this? I really need this.

6 years ago

@iagovar, I see two possible avenues:

Using plugin 'Page Inject'

  • Copy '/user/plugins/page-inject/page-inject.yaml' into folder '/user/config/plugins/'
  • In the copied config file, set property processed_content: false.
    This will tell the plugin to inject the raw table html into the page.
  • Copy your 'table.html' file into folder '/user/pages/includes/table/' and rename it 'table.md'.
  • Insert the following snippet into the content of the page in which you want to include 'table.md':
    TXT
    [plugin:content-inject](/includes/table)
    
  • The HTML content from 'table.md' will now be injected into the page 'as-is'

Using Twig templates:

  • Create an inherited theme.
  • In your inherited theme, create folder '/templates/includes'.
  • Copy your 'table.html' into that folder.
  • In the page in which you want to include 'table.html' add the following to the header:
    YAML
    process:
      twig: true
    
  • In the same page, insert somewhere in the content:
    TWIG
    {% include 'includes/table.html' %}
    
  • The HTML content from 'table.html' will now be injected into the page 'as-is'

Be advised that in both avenues, once the table has been inserted into the page, the page will not refresh its contents when you change the table. It has been cached. You will need to invalidate the cache to refresh the contents.

6 years ago

Thank you, I'll try both although the Twig one seems less mantaining effort.

Also, is there any way to purge cache or disable cache for specific pages?

6 years ago

@iagovar,

Prevent caching of page:

  • Add the following to frontmatter of page:
    TXT
    cache_enable: false
    

    Invalidating the cache:

  • Save page with included html
    This will invalidate single page.
  • $ touch /user/pages/01.home/default.md
    This will invalidate single page.
  • $ touch /user/config/system.yaml
  • $ bin/grav cache
  • Delete files in '/cache' folder
  • ...

Suggested topics

Topic Participants Replies Views Activity
Support · by Water Science, 3 weeks ago
11 170 7 hours ago
Support · by Duc , 22 hours ago
1 44 22 hours ago
Support · by Thomas, 1 week ago
3 116 1 day ago
Support · by Anna, 4 days ago
2 109 2 days ago
Support · by Justin Young, 2 days ago
1 89 2 days ago