Heat the olive oil in a large pan over medium heat. Add the onions and cook for 4-5 minutes or until softened. Add the garlic and cook for 1 more minute.
Stir in the red pepper flakes (optional), then add the pureed tomatoes. Season with salt and pepper to taste.
Bring to a simmer and cook for 20 minutes or until sauce has just started to thicken. Stir in the sprigs of basil, then remove the sauce from the heat.
While the sauce is simmering, cook the spaghetti in a large pot of salted water according to package directions.
Drain the pasta, reserving 1/4 cup of the pasta cooking liquid.
Discard the basil sprigs. Add the pasta to the pan with the tomato sauce, toss to coat. Place the pan of pasta over medium heat.
Add the pasta water, 1 tablespoon at a time, until pasta sauce is at your desired consistency.
Stir in the butter and half of the parmesan cheese. Toss the pasta until the cheese and butter are melted.
Remove the pasta from the heat. Top with remaining parmesan cheese and fresh basil leaves, then serve immediately.