-
Notifications
You must be signed in to change notification settings - Fork 0
Generate custom social images from SVG templates via Linkable entities #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6be6016
a558e56
556d45a
876b47f
a72b5fc
28b8fd0
6caa9ad
eb55c8b
f0fe11f
f3e6fb9
d6d58fc
a1cf0cd
309cad9
90dd6f2
739567e
eb3a62e
ad34271
daf3ca2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -44,3 +44,106 @@ the contents of which needs to be deployed to the `BASE_URL`. | |||||||
| ## Pages CI setup | ||||||||
|
|
||||||||
| Includes Gitlab CI / Gitlab Pages setup. | ||||||||
|
|
||||||||
| ## Features | ||||||||
|
|
||||||||
| ### Custom Social Images | ||||||||
|
|
||||||||
| Generate dynamic social images (Open Graph, Twitter Cards) from SVG templates. Each route can render a `.svg` version that is then processed through ImgProxy. | ||||||||
|
|
||||||||
| #### How It Works | ||||||||
|
|
||||||||
| 1. **SVG URLs**: Any Linkable entity can generate an SVG URL using `yassg_svg_url(entity)` | ||||||||
| 2. **Template Rendering**: The SVG template at `pages/{route}.svg.twig` receives the same context as the HTML page | ||||||||
| 3. **ImgProxy Processing**: Use `yassg_thumbnail()` to process the SVG through ImgProxy | ||||||||
| 4. **Build Time**: Images are generated and optimized during the build process | ||||||||
|
|
||||||||
| #### Usage in Templates | ||||||||
|
|
||||||||
| Define a `social_image` block in your page template that uses `yassg_svg_url()`: | ||||||||
|
|
||||||||
| ```twig | ||||||||
| {# templates/pages/article.html.twig #} | ||||||||
| {% extends 'layout.html.twig' %} | ||||||||
|
|
||||||||
| {% set article = yassg_find_one_by('articles', {condition: {'item.slug': slug}}) %} | ||||||||
|
|
||||||||
| {% block title %}{{ article.title }}{% endblock %} | ||||||||
|
|
||||||||
| {% block social_image %}{{ yassg_svg_url(article) }}{% endblock %} | ||||||||
|
|
||||||||
| {% block body %} | ||||||||
| {# ... #} | ||||||||
| {% endblock %} | ||||||||
| ``` | ||||||||
|
||||||||
|
|
||||||||
| In your layout template, use the block to generate meta tags: | ||||||||
|
|
||||||||
| ```twig | ||||||||
| {# templates/layout.html.twig #} | ||||||||
| {% block social_image %}{% endblock %} | ||||||||
| {% set social_image_url = block('social_image') %} | ||||||||
| {% if social_image_url is not empty %} | ||||||||
| <meta property="og:image" content="{{ yassg_thumbnail(absolute_url(social_image_url)) }}"> | ||||||||
| <meta name="twitter:image" content="{{ yassg_thumbnail(absolute_url(social_image_url)) }}"> | ||||||||
| <meta name="twitter:card" content="summary_large_image"> | ||||||||
| {% endif %} | ||||||||
| ``` | ||||||||
|
|
||||||||
| #### Creating SVG Templates | ||||||||
|
|
||||||||
| Create SVG templates in `templates/pages/{route}.svg.twig`: | ||||||||
|
|
||||||||
| ```svg | ||||||||
| {# templates/pages/article.svg.twig #} | ||||||||
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 630"> | ||||||||
| <rect width="1200" height="630" fill="#1a202c"/> | ||||||||
| <text x="600" y="315" font-family="Arial" font-size="64" fill="#ffffff" text-anchor="middle"> | ||||||||
| {{ article.title }} | ||||||||
| </text> | ||||||||
| <text x="600" y="380" font-family="Arial" font-size="24" fill="#cccccc" text-anchor="middle"> | ||||||||
| {{ article.publishedAt|date('F j, Y') }} | ||||||||
| </text> | ||||||||
| </svg> | ||||||||
| ``` | ||||||||
|
|
||||||||
| The SVG template receives the same variables as the HTML template (article, page, etc.). | ||||||||
|
|
||||||||
|
Comment on lines
+108
to
+111
|
||||||||
| ``` | |
| The SVG template receives the same variables as the HTML template (article, page, etc.). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in daf3ca2. Removed the malformed code fragment (format: 'png' and extra closing braces/backticks).
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| when@dev: | ||
| web_profiler_wdt: | ||
| resource: '@WebProfilerBundle/Resources/config/routing/wdt.xml' | ||
| resource: '@WebProfilerBundle/Resources/config/routing/wdt.php' | ||
| prefix: /_wdt | ||
|
|
||
| web_profiler_profiler: | ||
| resource: '@WebProfilerBundle/Resources/config/routing/profiler.xml' | ||
| resource: '@WebProfilerBundle/Resources/config/routing/profiler.php' | ||
| prefix: /_profiler |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,14 @@ | |||||
|
|
||||||
| <title>{{ demo_page.title }}</title> | ||||||
|
|
||||||
| {% block social_image %}{% endblock %} | ||||||
|
||||||
| {% block social_image %}{% endblock %} | |
| {% set social_image_url = yassg_thumbnail(yassg_svg_url(demo_page), {width: 1200, height: 630, format: 'webp'}) %} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 630" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);"> | ||
| <defs> | ||
| <style> | ||
| .title { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; font-size: 72px; font-weight: bold; fill: #ffffff; } | ||
| .subtitle { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; font-size: 36px; fill: #f0f0f0; } | ||
| </style> | ||
| <linearGradient id="bgGradient" x1="0%" y1="0%" x2="100%" y2="100%"> | ||
| <stop offset="0%" style="stop-color:#667eea;stop-opacity:1" /> | ||
| <stop offset="100%" style="stop-color:#764ba2;stop-opacity:1" /> | ||
| </linearGradient> | ||
| </defs> | ||
|
|
||
| <rect width="1200" height="630" fill="url(#bgGradient)"/> | ||
|
|
||
| <text x="600" y="280" class="title" text-anchor="middle"> | ||
| {% if demo_page is defined %} | ||
| {{ demo_page.title }} | ||
| {% else %} | ||
| My Awesome Site | ||
| {% endif %} | ||
| </text> | ||
|
|
||
| <text x="600" y="360" class="subtitle" text-anchor="middle"> | ||
| {% if demo_page is defined and demo_page.description is defined %} | ||
| {{ demo_page.description }} | ||
| {% else %} | ||
| Built with YASSG | ||
| {% endif %} | ||
| </text> | ||
| </svg> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| {% set article = yassg_find_one_by('articles', {condition: {'item.slug': slug}}) %} | ||
|
|
||
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 630"> | ||
| <rect width="1200" height="630" fill="#1a202c"/> | ||
| <text x="600" y="315" font-family="Arial, sans-serif" font-size="64" fill="#ffffff" text-anchor="middle"> | ||
| {{ article.title }} | ||
| </text> | ||
| <text x="600" y="380" font-family="Arial, sans-serif" font-size="24" fill="#cccccc" text-anchor="middle"> | ||
| {{ article.publishedAt|date('F j, Y') }} | ||
| </text> | ||
| </svg> |
Uh oh!
There was an error while loading. Please reload this page.