惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MyScale Blog
MyScale Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
Y
Y Combinator Blog
月光博客
月光博客
IT之家
IT之家
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
L
LangChain Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
博客园 - Franky
WordPress大学
WordPress大学
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
V
Visual Studio Blog
小众软件
小众软件
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
N
Netflix TechBlog - Medium

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Stop Hardcoding HTML for Every Image — Use Template Varia...
Özgür S. · 2026-06-15 · via DEV Community
Cover image for Stop Hardcoding HTML for Every Image — Use Template Variables Instead

Özgür S.

Most developers generate images like this:

const html = `
  <div style="...">
    <h1>${post.title}</h1>
    <p>${post.author} · ${post.date}</p>
  </div>
`;

This works — until it doesn't.

  • Special characters break the HTML (&, <, > in titles)
  • The template grows and the string becomes harder to maintain
  • Reusing the same template across environments gets messy
  • No clear separation between design and data

A cleaner approach: template variables

Instead of interpolating values into HTML strings, pass them separately:

const response = await fetch('https://renderpix.dev/v1/render', {
  method: 'POST',
  headers: {
    'X-API-Key': 'rpx_your_key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    html: `
      <div style="width:1200px;height:630px;background:#0f172a;
        display:flex;flex-direction:column;justify-content:center;
        padding:80px;font-family:sans-serif">
        <div style="color:#22d3ee;font-size:18px">{{domain}}</div>
        <div style="color:white;font-size:52px;font-weight:bold;line-height:1.2">
          {{title}}
        </div>
        <div style="color:#94a3b8;font-size:20px;margin-top:24px">
          {{author}} · {{date}}
        </div>
      </div>
    `,
    vars: {
      title: post.title,
      author: post.author,
      date: post.publishedAt,
      domain: 'myblog.com'
    },
    width: 1200,
    height: 630,
    format: 'png'
  })
});

The HTML template stays clean. Values are injected at render time — no string interpolation, no escaping issues.

How it works

RenderPix does a find-and-replace before rendering the page:

  • {{title}} → value of vars.title
  • {{author}} → value of vars.author
  • Missing key? The placeholder stays as-is — no errors thrown.

This means you can store your HTML template as a file, an environment variable, or a database field, and just swap the vars object per request.

The real power: combine with batch rendering

Template variables get interesting when you need multiple images at once.

One HTML template + N different vars objects = N unique images, one API request:

const OG_TEMPLATE = `
  <div style="width:1200px;height:630px;background:#0f172a;
    display:flex;flex-direction:column;justify-content:center;padding:80px">
    <div style="color:#22d3ee;font-size:18px;margin-bottom:16px">{{domain}}</div>
    <div style="color:white;font-size:52px;font-weight:bold;line-height:1.2">{{title}}</div>
    <div style="color:#94a3b8;font-size:20px;margin-top:24px">{{author}} · {{date}}</div>
  </div>
`;

const response = await fetch('https://renderpix.dev/v1/batch', {
  method: 'POST',
  headers: {
    'X-API-Key': 'rpx_your_key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    items: blogPosts.map(post => ({
      html: OG_TEMPLATE,
      vars: {
        title: post.title,
        author: post.author,
        date: post.date,
        domain: 'myblog.com'
      },
      width: 1200,
      height: 630,
      format: 'png'
    }))
  })
});

const { results } = await response.json();
// results[0].url, results[1].url, ...
// 50 unique OG images. One request.

Without template variables, this would be 50 different HTML strings to maintain. With them, it's one.

Use cases

This pattern works well anywhere you have a fixed design and variable data:

  • OG images — one template per blog, dynamic per post
  • Certificates — one template per course, dynamic per student
  • Invoices — one template per company, dynamic per transaction
  • Social cards — one brand template, different content daily

Available on all plans

Template variables work on free, starter, pro, and scale — no upgrade needed to try it.

If you want to test it: renderpix.dev gives you 100 free renders/month, no credit card required.