dart-archive / shelf_static

archived repo
https://github.com/dart-lang/shelf/tree/master/pkgs/shelf_static
BSD 3-Clause "New" or "Revised" License
24 stars 24 forks source link

feat(shelf_static): Add a template engine to inject values on static files #72

Closed albertodev01 closed 2 years ago

albertodev01 commented 2 years ago

I haven't been able to find this feature but I'm willing to help with a PR if needed!


Imagine having this page in a file called index.html:

<html>
<body>
<p>Hello, my name is {:name} and I am {:my_age} years old</p>
</body>
</html>

I would like to be able to replace {:name} and {:my_age} with some values coming from Dart, like this:

final indexPageValues = PageValues(
  file: 'index.html',
  values: {
    'name': 'Alberto',
    'my_age': 25,
  },
);

shelf_static.createStaticHandler(
  'public', 
  defaultDocument: 'index.html',

  // add this param
  pageValues: <PageValues>[
    indexPageValues,
  ],
);

In this way, the {:some_name} tags in the HTML code will be replaced by the values provided via Dart. The result would be

<html>
<body>
<p>Hello, my name is Alberto and I am 25 years old</p>
</body>
</html>

This would also allow custom HTML processed from Dart.


Please take the the {:some_name} syntax as example, don't focus on it. Focus on the general idea instead: might this be good? šŸ™‚ what could I improve to make this look better?

Thank you!

kevmoo commented 2 years ago

Building such a feature would be straight-forward, but that wouldn't fit into shelf_static. This package is explicitly (and narrowly) focused on being a solid static file handler.

albertodev01 commented 2 years ago

Where could it be implemented then? Would it be in an existing package or in a brand new one?

kevmoo commented 2 years ago

I'd do it in a new package. Look at https://pub.dev/packages/shelf_mustache

On Wed, Feb 23, 2022 at 10:56 AM Alberto @.***> wrote:

Where could it be implemented then? Would it be in an existing package or in a brand new one?

ā€” Reply to this email directly, view it on GitHub https://github.com/dart-lang/shelf_static/issues/72#issuecomment-1049106701, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAEFCQ54X4G32WHGCORRYTU4UUV5ANCNFSM5PELCKUQ . You are receiving this because you modified the open/close state.Message ID: @.***>