There are a bunch of questions on Stack Overflow about whether AJAX should return JSON or HTML, and most seem to agree that it is ideal to return JSON for the sake of speed. However, that means that if I degrade gracefully, I will have some duplicate code because I am generating the same markup in both PHP and Javascript.
A hypothetical example: A website has a list of links to short stories. If the user has Javascript, then clicking on one of these links loads the story without a page refresh. This is done with an AJAX request that returned a JSON with the story information. Javascript generates the markup for the story. If the user does not have Javascript, then clicking on the same link reloads the page with the story now loaded. PHP generates the markup for the story.
Is there a solution to use JSON and degrade gracefully without duplicating the code?